Created
July 7, 2015 17:59
-
-
Save lattmann/5bb0f595355128022f65 to your computer and use it in GitHub Desktop.
Difference between Q all and Q allSettled
This file contains hidden or 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'), | |
id = 0; | |
function doWork(timeout, shouldFail) { | |
var deferred = Q.defer(), | |
_id; | |
id += 1; | |
_id = id; | |
console.log(_id + ' should fail: ' + (shouldFail ? true : false).toString()); | |
setTimeout(function () { | |
console.log(_id + ' did work'); | |
if (shouldFail === true) { | |
deferred.reject('Failed'); | |
} else { | |
deferred.resolve(_id); | |
} | |
}, timeout); | |
return deferred.promise; | |
} | |
function functionName(fun) { | |
var ret = fun.toString(); | |
ret = ret.substr('function '.length); | |
ret = ret.substr(0, ret.indexOf('(')); | |
return ret; | |
} | |
function run(allSettled) { | |
var fn = allSettled ? Q.allSettled : Q.all; | |
console.log('Using Q.' + functionName(fn)); | |
fn([ | |
doWork(200, false), | |
doWork(100, true), | |
doWork(300, false), | |
doWork(150, true) | |
]) | |
.then(function (results) { | |
console.log('called then'); | |
}) | |
.catch(function () { | |
console.log('called catch'); | |
}); | |
} | |
run(true); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment