Reputation: 1166
I'm playing with Promise.
This is my code :
APC.UTIL.AutoDOM = {
step: -1, PROMISES : [],
add: function (fun) {
this.PROMISES.push (fun);
},
run: function () {
this.step = 0; this.nn = this.PROMISES.length-1;
this.PROMISES.push ( function() {this.end();});
Promise.all( this.PROMISES).then (
function ( ) { APC.UTIL.AutoDOM.step++; console.log("OK"+APC.UTIL.AutoDOM.step);}
).catch(
function(err) { console.log(err);}
);
},
end: function () {
this.PROMISES.clear();
}
}
And used so :
APC.UTIL.AutoDOM.add ( function () {console.log("hello1");} );
APC.UTIL.AutoDOM.add ( function () {console.log("hello2");} );
APC.UTIL.AutoDOM.add ( function () {console.log("hello3");} );
APC.UTIL.AutoDOM.run();
Only works one time....
OK1
Anybody can fix my code ? TH
Upvotes: 0
Views: 75
Reputation: 382434
What you have here in PROMISES isn't an array of promises but an array of functions. If your goal is to have all those functions executed (possibly in parallel) then to have another function executed with the result of each one, you can do this :
Promise
.map(this.PROMISES, function(f){ f() })
.map(function(_, i){ console.log('OK'+(i+1)) })
.finally(function(){ this.PROMISES.clear(); });
Disclaimer : tested with Bluebird
Upvotes: 1