Skip to content

Loading…

Future.wrap upgrade #159

Closed
wants to merge 3 commits into from

2 participants

@fresheneesz
  • no longer cares about a function's length property (just appends assumes the last parameter will be the callback)
  • Allows passing in an object reference for method calls

Related to: #127

@laverdet
Owner

node build -f does this?

Bah, it seems like you're right. I could have sworn I tried all those instructions..

@fresheneesz

@laverdet You gonna merge in the wrap function fix?

@laverdet
Owner

Yeah here's my version: 969e81d

@laverdet laverdet closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jun 16, 2013
  1. @fresheneesz
Commits on Feb 13, 2014
  1. fixing the wrap function laverdet#127

    bt committed
  2. Revert "adding easier way to rebuild on windows"

    bt committed
    This reverts commit de9b1ad.
Showing with 23 additions and 12 deletions.
  1. +23 −12 future.js
View
35 future.js
@@ -17,20 +17,31 @@ function Future() {}
/**
* Wrap a node-style async function to return a future in place of using a callback.
+ * either used like futureWrap(function(){ ... })(arg1,arg2,etc) or
+ * futureWrap(object, 'methodName')(arg1,arg2,etc)
*/
-Future.wrap = function(fn, idx) {
- idx = idx === undefined ? fn.length - 1 : idx;
+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);
- if (args.length > idx) {
- throw new Error('function expects no more than '+ idx+ ' arguments');
- }
- var future = new Future;
- args[idx] = future.resolver();
- fn.apply(this, args);
- return future;
- };
-};
+ 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
+ }
+}
/**
* Wait on a series of futures and then return. If the futures throw an exception this function
Something went wrong with that request. Please try again.