/
Async.js
35 lines (32 loc) · 1.21 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
// Copyright 2015 by Thorsten von Eicken
// Asynchronous helpers
exports.sequence = function(funcs, scope, end) {
scope = scope||{};
scope.next = function() {
//console.log("SEQ: next of", funcs.length);
funcs.length > 0 ? funcs.shift().apply(scope, arguments) : end.call(scope);
};
scope.end = function() { funcs=[]; scope.next(); };
scope.next();
}
// sequence: creates a sequence of tasks, each of which ends up calling next() to invoke the next one.
// From: http://forum.espruino.com/conversations/260159/
//exports.sequence = function(funcs,scope,done) {
// var _seq = 0,stopped = false;
// this.scope = scope;
// this.next = function(){this.goto(_seq,arguments);};
// this.goto = function(seq,args){
// if(seq < funcs.length){
// if(!stopped){ funcs[seq](args);_seq = seq + 1;} }
// else{done(args);}
// };
// this.exit = function(){stopped = true;done(arguments);};
// this.next();
//};
// concurrence: creates parallel tasks, all of which end up calling next().
exports.concurrence = function(funcs,scope,done) {
var _num = funcs.length;
this.scope = scope;
this.next = function(){ if (--_num <= 0) done();}
funcs.every(function(i){i();});
};