+26
-27
@@ -36,3 +36,3 @@ /** | ||
| * Func is passed just a standard callback taking err and ret. | ||
| * Returns via its callback the truthy value from func. | ||
| * Returns to the callback the error or the truthy value from func. | ||
| */ | ||
@@ -44,33 +44,32 @@ function repeatUntil( func, callback ) { | ||
| var alreadyReturned; | ||
| var callDepth = 0; | ||
| function _loop( callDepth ) { | ||
| function _invokeCallback( err, stop ) { | ||
| if (err || stop) { | ||
| if (alreadyReturned) throw new Error("callback already called"); | ||
| alreadyReturned = true; | ||
| return callback(err, stop); | ||
| } | ||
| else if (++callDepth < 40) { | ||
| _loop(); | ||
| } | ||
| else { | ||
| callDepth = 0; | ||
| qflowSetImmediate(_loop); | ||
| } | ||
| } | ||
| function _loop( ) { | ||
| try { | ||
| func( function(err, stop) { | ||
| if (err || stop) { | ||
| alreadyReturned = err || stop; | ||
| return callback(err, stop); | ||
| } | ||
| else if (callDepth < 40) { | ||
| _loop(callDepth + 1); | ||
| } | ||
| else { | ||
| qflowSetImmediate(function() { _loop(0); }); | ||
| } | ||
| } ); | ||
| func( _invokeCallback ); | ||
| } | ||
| catch (e) { | ||
| // be sure to not call the callback more than once, and do not | ||
| // inject errors from within the callback back into the callback. | ||
| // If the callback throws while still in our try/catch block, | ||
| // re-throw it as if it were coming from inside a setImmediate. | ||
| if (!alreadyReturned) { | ||
| callback(alreadyReturned = e); | ||
| } | ||
| else { | ||
| throw e; | ||
| } | ||
| catch (err) { | ||
| // do not inject errors from within the callback back into the callback. | ||
| // If func returns without yielding, the callback will still be in | ||
| // our try/catch block. Re-throw that error like setImmediate would do. | ||
| // But if func has not returned yet, the error is from func, so return it. | ||
| if (alreadyReturned) throw err; else _invokeCallback(err); | ||
| } | ||
| } | ||
| _loop(0); | ||
| _loop(); | ||
| } | ||
@@ -77,0 +76,0 @@ |
+1
-1
| { | ||
| "name": "aflow", | ||
| "version": "0.9.3", | ||
| "version": "0.10.0", | ||
| "description": "async flow control for calls with callbacks", | ||
@@ -5,0 +5,0 @@ "license": "Apache-2.0", |
+5
-0
@@ -167,2 +167,7 @@ aflow | ||
| 0.10.0 | ||
| - make it a fatal error if callback is called more than once | ||
| - refactor for large speedup | ||
| 0.9.3 | ||
@@ -169,0 +174,0 @@ |
26348
0.35%195
2.63%559
-0.18%