Created
October 1, 2012 17:38
-
-
Save jtwb/3813234 to your computer and use it in GitHub Desktop.
Exploring q.js flow control library: comparing ncall/nbind and promises
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'); | |
/* | |
* ncall / nbind test | |
*/ | |
function simpleAdmin(callback) { | |
setTimeout( | |
function () { | |
console.log('nc admin ok'); | |
callback(); | |
}, | |
300 | |
); | |
}; | |
function simpleDocument(opts, callback) { | |
console.log(arguments); | |
//console.log( new q.defer().makeNodeResolver() ); | |
setTimeout( | |
function () { | |
console.log('nc doc ok'); | |
console.log(callback); | |
callback(); | |
}, | |
600 | |
); | |
}; | |
/* | |
q.all([ | |
q.ncall(simpleAdmin), | |
q.ncall(simpleDocument) | |
]).then(function() { | |
console.log(arguments); | |
console.log('nc both done'); | |
}); | |
*/ | |
q.ncall(simpleAdmin) | |
.then(q.nbind(simpleDocument)) | |
.then(function() { | |
console.log('nc both done'); | |
}); | |
/* | |
* deferred test | |
*/ | |
function getAdminAccount() { | |
var defer = q.defer(); | |
setTimeout( | |
function () { | |
console.log('admin ok'); | |
defer.resolve() | |
}, | |
300 | |
); | |
return defer.promise; | |
}; | |
function getDocumentObject() { | |
var defer = q.defer(); | |
setTimeout( | |
function () { | |
console.log('doc ok'); | |
defer.resolve() | |
}, | |
600 | |
); | |
return defer.promise; | |
}; | |
q.all([ | |
getAdminAccount(), | |
getDocumentObject() | |
]).then(function() { | |
console.log('both done'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment