Created
April 20, 2015 14:38
-
-
Save craigp/1c4806a23cf576192dc5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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