Skip to content

Instantly share code, notes, and snippets.

@jtwb
Created October 1, 2012 17:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jtwb/3813234 to your computer and use it in GitHub Desktop.
Save jtwb/3813234 to your computer and use it in GitHub Desktop.
Exploring q.js flow control library: comparing ncall/nbind and promises
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