Skip to content

Instantly share code, notes, and snippets.

@assertnotnull
Last active August 29, 2015 14:20
Show Gist options
  • Save assertnotnull/64714c0f3503d80bf70e to your computer and use it in GitHub Desktop.
Save assertnotnull/64714c0f3503d80bf70e to your computer and use it in GitHub Desktop.
Q promises concurrency test
var Q = require('q');
var debug = require('debug')('test');
var func1 = function() {
return Q.promise(function(resolve, reject) {
setTimeout(function() {
debug('concurrent func1?');
resolve(1);
}, 4000);
});
};
var func2 = function() {
return Q.promise(function(resolve, reject) {
setTimeout(function() {
debug('concurrent func2?');
resolve(2);
}, 2000);
});
};
var func3 = function() {
return Q.promise(function(resolve, reject) {
setTimeout(function() {
debug('sequential func3?');
resolve(3);
}, 7000);
});
};
Q.all([
func1().then(function (fvalue) {
debug('done with one');
return func3().then(function (value) {
return fvalue + value;
});
}),
func2()
]).then(function(response) {
debug('done concurrent');
debug(response);
return response;
})
.fin(function() {
debug('finally');
})
.done(function success(argument) {
debug('success', argument);
return argument;
}, function (error) {
debug('error :', error)
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment