Skip to content

Instantly share code, notes, and snippets.

@heymatthew
Created April 1, 2013 02:26
Show Gist options
  • Save heymatthew/5282912 to your computer and use it in GitHub Desktop.
Save heymatthew/5282912 to your computer and use it in GitHub Desktop.
Testing then statement in the Q framework to assert then() return's a wrapped promise callback. "Give me a callback for this promise and I'll give you a promise that represents the result of that callback."
(function() {
q = require('q');
var one,two,three;
function asyncOne() {
console.log('one called');
var allDone = q.defer();
setTimeout(allDone.resolve, 1000);
return allDone.promise;
}
function asyncTwo() {
console.log('two called');
var allDone = q.defer();
setTimeout(allDone.resolve, 1000);
return allDone.promise;
}
function asyncThree() {
console.log('three called');
var allDone = q.defer();
setTimeout(allDone.resolve, 1000);
return allDone.promise;
}
console.log("Testing 'then' mimics jQuery's pipe");
var oneDone = asyncOne();
var twoDone = oneDone.then( asyncTwo );
var threeDone = twoDone.then( asyncThree );
oneDone.then( function() { console.log('one is done') } );
twoDone.then( function() { console.log('two is done') } );
threeDone.then( function() { console.log('three is done') } );
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment