Last active
December 15, 2015 20:19
-
-
Save kpuputti/5317952 to your computer and use it in GitHub Desktop.
Example of lazy promises ( https://github.com/nathan7/lazy-promise )
This file contains hidden or 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'), | |
LazyPromise = require('lazy-promise'); | |
function async(name) { | |
console.log('start async: ' + name); | |
var deferred = Q.defer(); | |
// Resolve the promise in the next turn of the event loop | |
process.nextTick(function () { | |
console.log('finish async: ' + name); | |
deferred.resolve(); | |
}); | |
return deferred.promise; | |
} | |
// =========================================== // | |
console.log('= create promises ='); | |
var promise1 = async('promise1'); | |
console.log('got promise1'); | |
var promise2 = new LazyPromise(function (resolve, reject) { | |
async('promise2').then(resolve, reject); | |
}); | |
console.log('got promise2'); | |
// =========================================== // | |
console.log('= adding resolvage handlers ='); | |
promise1.then(function () { | |
console.log('resolved promise1'); | |
}); | |
promise2.then(function () { | |
console.log('resolved promise2'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment