Skip to content

Instantly share code, notes, and snippets.

@craigp
Created April 20, 2015 14:38
Show Gist options
  • Save craigp/1c4806a23cf576192dc5 to your computer and use it in GitHub Desktop.
Save craigp/1c4806a23cf576192dc5 to your computer and use it in GitHub Desktop.
var Q = require('q'),
_ = require('lodash');
var eventualAdd = function(n1, n2) {
return Q.promise(function(resolve, reject, notify) {
var n = setInterval(function() {
notify("working");
}, 100);
setTimeout(function() {
clearInterval(n);
resolve(n1 + n2);
}, 500);
});
};
var res = 0;
var nums = [1,2,3,4,5,6,7,8];
nums = _.chunk(nums, 2);
var promises = [];
for (var i = 0, len = nums.length; i < len; i++) {
var set = nums[i];
promises.push(eventualAdd(set[0], set[1]));
}
Q.all(promises)
.spread(function(a,b,c,d) {
console.log(a,b,c,d);
})
.progress(function(msg) {
console.log(msg.value);
})
.catch(function(err) {
console.log("ERROR", err);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment