Last active
August 29, 2015 14:20
-
-
Save assertnotnull/64714c0f3503d80bf70e to your computer and use it in GitHub Desktop.
Q promises concurrency test
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'); | |
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