Skip to content

Instantly share code, notes, and snippets.

@lattmann
Created July 7, 2015 17:59
Show Gist options
  • Save lattmann/5bb0f595355128022f65 to your computer and use it in GitHub Desktop.
Save lattmann/5bb0f595355128022f65 to your computer and use it in GitHub Desktop.
Difference between Q all and Q allSettled
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