How is Future.wrap supposed to be used? #127

Closed
fresheneesz opened this Issue Jul 12, 2013 · 7 comments

Comments

Projects
None yet
3 participants
@fresheneesz
Contributor

fresheneesz commented Jul 12, 2013

It seems like you use Future.wrap like this:

var f = Future.wrap(someAsyncFunction, 2)('a', 'b');

If I don't specify idx, running it always gives the error: "function expects no more than -1 arguments". This seems wrong...

But why require specifying the number of parameters in the first place? Couldn't you rewrite like this without any real downside?:

function(fn) {
  return function() {
    var args = Array.prototype.slice.call(arguments);
    var future = new Future;
    args.push(future.resolver());
    fn.apply(this, args);
    return future;
  };
};

I wrote a function I like a little better:

// either used like futureWrap(function(){ ... })(arg1,arg2,etc) or
//  futureWrap(object, 'methodName')(arg1,arg2,etc)
Future.wrap = function() {
    // function
    if(arguments.length === 1) {
        var fn = arguments[0];
        var object = undefined;

    // object, function
    } else {
        var object = arguments[0];
        var fn = object[arguments[1]];
    }

    return function() {
        var args = Array.prototype.slice.call(arguments);
        var future = new Future;
        args.push(future.resolver());
        var me = this;
        if(object) me = object;
        fn.apply(me, args);
        return future;
    };
};

That way you can also call object methods pretty directly:

var db = mongo.db('localhost/local', {journal: true});
Future.wrap(db.collection('user').find(), 'toArray')().wait()

Thoughts?

@fresheneesz

This comment has been minimized.

Show comment Hide comment
@fresheneesz

fresheneesz Oct 24, 2013

Contributor

@laverdet Please comment

Contributor

fresheneesz commented Oct 24, 2013

@laverdet Please comment

@laverdet

This comment has been minimized.

Show comment Hide comment
@laverdet

laverdet Oct 25, 2013

Owner

If I don't specify idx, running it always gives the error: "function expects no more than -1 arguments". This seems wrong...

If you get this message it means the .length property on someAsyncFunction is incorrect.

The method you posted where the callback is just appended to the function parameters is probably better in the long run.

Owner

laverdet commented Oct 25, 2013

If I don't specify idx, running it always gives the error: "function expects no more than -1 arguments". This seems wrong...

If you get this message it means the .length property on someAsyncFunction is incorrect.

The method you posted where the callback is just appended to the function parameters is probably better in the long run.

@fresheneesz

This comment has been minimized.

Show comment Hide comment
@fresheneesz

fresheneesz Oct 26, 2013

Contributor

Is that something you'd like me to create a pull request for?

Contributor

fresheneesz commented Oct 26, 2013

Is that something you'd like me to create a pull request for?

@laverdet

This comment has been minimized.

Show comment Hide comment
@laverdet

laverdet Oct 26, 2013

Owner

Sure

Owner

laverdet commented Oct 26, 2013

Sure

fresheneesz added a commit to fresheneesz/node-fibers that referenced this issue Feb 13, 2014

@laverdet

This comment has been minimized.

Show comment Hide comment
@laverdet

laverdet Oct 22, 2014

Owner

A new version of wrap was committed in 969e81d

Owner

laverdet commented Oct 22, 2014

A new version of wrap was committed in 969e81d

@laverdet laverdet closed this Oct 22, 2014

@JThobbyist

This comment has been minimized.

Show comment Hide comment
@JThobbyist

JThobbyist Mar 12, 2018

can somebody please explain how to use future.wrap with a function that takes 'arg1' as an argument and has a callback(error,response,body)? (like with http requests using the 'request' package)

I want to do something like

var body = future.wrap(asyncFunc,???)(args???).body

is multi supposed to be an integer representing the arguments in the callback after error? an array specifying the name of each argument in the callback? and are the arguments in the (arg???) are they the arguments of the function that I want to execute or the arguments of the callback function??
do i need to write
var futFunc = future.wrap(asyncFunc,2)('response','body');
body = futFunc(argForasyncFunc).body;
???

Please explain, because the docs are so unclear as to the correct usage of this

JThobbyist commented Mar 12, 2018

can somebody please explain how to use future.wrap with a function that takes 'arg1' as an argument and has a callback(error,response,body)? (like with http requests using the 'request' package)

I want to do something like

var body = future.wrap(asyncFunc,???)(args???).body

is multi supposed to be an integer representing the arguments in the callback after error? an array specifying the name of each argument in the callback? and are the arguments in the (arg???) are they the arguments of the function that I want to execute or the arguments of the callback function??
do i need to write
var futFunc = future.wrap(asyncFunc,2)('response','body');
body = futFunc(argForasyncFunc).body;
???

Please explain, because the docs are so unclear as to the correct usage of this

@laverdet

This comment has been minimized.

Show comment Hide comment
@laverdet

laverdet Mar 21, 2018

Owner

multi is a boolean which when set will cause the future to resolve to an array of non-err arguments. If the callback signatures is function(err, response, body) and you wrap the async function with multi:true, the future will resolve to [response, body].

Owner

laverdet commented Mar 21, 2018

multi is a boolean which when set will cause the future to resolve to an array of non-err arguments. If the callback signatures is function(err, response, body) and you wrap the async function with multi:true, the future will resolve to [response, body].

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment