How can I pass a callback to a generator that I pass to the "vo" library?
Asked Answered
S

3

7

I am trying to understand how callbacks work so I created a function and passed a second argument named 'callback', which I call at the end of the function with 'callback(arr)' . However I am getting an error which says : "callback is not a function" ? Can you please tell me what I am doing wrong?

UPDATE

vo is a nodejs library that takes a generator function* () and runs all it's yields . It's basically a way to handle async code with less callbacks (yes I know I used a callback as well but that's pretty much a choice). A more popular library that does exactly the same thing is co. Link to vo: https://github.com/matthewmueller/vo

var Nightmare = require('nightmare');
var vo = require('vo');

function* MyFunction(query, callback) {
    arr = [];
    for (i = 0; i < 1; i++) {
        arr.push(yield Nightmare({
                show: true
            }).goto(`http://google.com`)
            .inject('js', 'jquery-3.1.0.js')
            .evaluate(() => {
                var title;
                title = 1
                extend = 2
                var img;
                img = 3
                var par;
                par = 4
                url = window.location.href;
                var par_arr = [5, 5, 5, 5];


                return {
                    title: title,
                    img: img,
                    par: par,
                    par_arr: par_arr,
                    url: url
                }

            }).end()
            .catch(function(error, nightmare) {

                console.error('Search failed:', error);
            }))
    }

    callback(arr);

    return arr;
}

vo(MyFunction)('query', (arr) => {
    console.log(arr);
});
Shavers answered 4/9, 2016 at 11:14 Comment(2)
Why would you pass a callback to the generator rather than to .then on the returned promise?Indistinctive
co appears not only to be more popular but also better documented.Anastasius
H
0

This is working in my enviroment;

var vo = require('vo');

function* idMaker(query, params){

  for (var i = 0; i < 5; i++) {
        console.log(query);
  }
 params.callback("callback value");
  return;
}

vo(idMaker)("param value",{callback: (value)=>console.log(value)}  );

Output:

param value
param value
param value
param value
param value
callback value

I have no time right now to read 'vo' docs to tell you why but I saw a exaplme here.

Hysterotomy answered 13/9, 2016 at 10:36 Comment(0)
W
0

vo calls the function you give to it without arguments. That's why you get the error you are getting.

What you need to do is pass to vo a function that when called will call MyFunction('query', (arr) => { console.log(arr); }).

Anonymous generator

You could do it like this:

vo(function *() {
    return (yield* MyFunction('query', (arr) => {
        console.log("callback", arr);
    }));
}).then((arr) => {
    console.log("then", arr);
});

Using co-bind

Or as the FAQ suggests, you can use co-bind Add the following require:

var cobind = require('co-bind');

And the the code above can become:

vo(cobind(MyFunction, undefined, 'query', (arr) => {
    console.log("callback", arr);
})).then((arr) => {
    console.log("then", arr);
});

Using co-bind rather than just doing MyFunction.bind is necessary because otherwise, vo won't treat the function as a generator.

Whether you use an anonymous generator or co-bind, the output I get is:

callback [ { img: 3,
    par: 4,
    par_arr: [ 5, 5, 5, 5 ],
    title: 1,
    url: 'https://www.google.com/?gws_rd=ssl' } ]
then [ { img: 3,
    par: 4,
    par_arr: [ 5, 5, 5, 5 ],
    title: 1,
    url: 'https://www.google.com/?gws_rd=ssl' } ]

This being said, I don't see any benefit to using a callback. I would just use .then() after vo without passing a callback to MyFunction. But even if you do not pass a callback, you still need to pass the query parameter to MyFunction, and can use one of the methods above.

Wahl answered 6/9, 2016 at 19:13 Comment(0)
H
0

This is working in my enviroment;

var vo = require('vo');

function* idMaker(query, params){

  for (var i = 0; i < 5; i++) {
        console.log(query);
  }
 params.callback("callback value");
  return;
}

vo(idMaker)("param value",{callback: (value)=>console.log(value)}  );

Output:

param value
param value
param value
param value
param value
callback value

I have no time right now to read 'vo' docs to tell you why but I saw a exaplme here.

Hysterotomy answered 13/9, 2016 at 10:36 Comment(0)
T
-2
vo(MyFunction)('query', (arr) => {
    console.log(arr);
});

This looks wrong. You're feeding vo MyFunction and then presumably it returns a function you then fire with 'query' and the fat arrow function as args. MyFunction would only work if vo returned MyFunction which would be kind of silly unless it were doing something to modify MyFunction in which case we really need to know what the heck vo is actually doing in order to answer this question.

Tomlinson answered 5/9, 2016 at 14:40 Comment(3)
vo is a function that takes a generator function* () and runs all it's yields . It's basically a way to handle async code without callbacks. A more popular library that does exactly the same thing is co. Link to vo: github.com/matthewmueller/voShavers
@Shavers "It's basically a way to handle async code without callbacks" except it still uses callbacks..Indistinctive
@Erik Returning a closure from vo would be entirely sensible. There's nothing wrong with this call pattern.Anastasius

© 2022 - 2024 — McMap. All rights reserved.