-
Notifications
You must be signed in to change notification settings - Fork 16
/
async.js
72 lines (64 loc) · 1.91 KB
/
async.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/**
* A library for leveraging generator syntax to making promises easier to work with.
* This is not a core dependency of Pintura.
* Example usage:
*
var async = require("./async").async;
sleep = function(time){
var future = new Future();
setTimeout(future.fulfill, time);
return future;
};
// some examples
sleepAndAdd4 = async(function(num){
yield sleep(1000);
returns = num + 4;
});
add5 = async(function(num){
var sum = yield sleepAndAdd4(num + 1);
console.log("sum " + sum);
returns = sum;
});
var add5Promise = add5(10);
sleepAndAdd4(10).addCallback(function(value){
console.log("callback the manual way " + value);
});
*/
var Promise = require("./promise").Promise;
// the async library
exports.async = function(generatorFunction){
return function(){
var generator;
var future = new Future();
function finish(value){
finished = true;
callbacks.forEach(function(callback){
callback(returnValue);
});
}
function executeUntilYield(value){
try{
while(true){
returns = undefined;
value = generator[value === undefined ? "next" : "send"](value);
if(value instanceof Promise){
value.addCallback(function(next){
return executeUntilYield(next);
});
break;
}
}
}
catch(e if StopIteration){
future.fulfill(returns);
}
}
generator = generatorFunction.apply(this, arguments);
if(Object.prototype.toString.call(generator) == "[object Generator]"){
executeUntilYield();
}else{
future.fulfill(generator);
}
return future.promise;
}
};