Skip to content

Instantly share code, notes, and snippets.

@kpuputti
Last active December 15, 2015 20:19
Show Gist options
  • Save kpuputti/5317952 to your computer and use it in GitHub Desktop.
Save kpuputti/5317952 to your computer and use it in GitHub Desktop.
Example of lazy promises ( https://github.com/nathan7/lazy-promise )
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