Skip to content

Instantly share code, notes, and snippets.

@ghiden
Created June 25, 2015 19:56
Show Gist options
  • Save ghiden/ccbf414148a19bc76ab2 to your computer and use it in GitHub Desktop.
Save ghiden/ccbf414148a19bc76ab2 to your computer and use it in GitHub Desktop.
Bluebird delay behaviors
var Promise = require('bluebird');
function test1() {
return new Promise(function(resolve) {
setTimeout(function() {
console.log('resolve');
resolve(100);
}, 500);
});
}
var result = test1().delay(500);
console.log(result.isPending());
setTimeout(function() {
console.log(result.isPending());
}, 800);
setTimeout(function() {
console.log(result.isPending());
console.log(result.value());
}, 1500);
///////////////////////////////////////////////
function test2() {
return new Promise(function(resolve, reject) {
setTimeout(function() {
console.log('reject');
reject(new Error('failed'));
}, 500);
});
}
setTimeout(function() {
console.log('\n\ntesting reject');
var result = test2().delay(500);
console.log(result.isPending());
setTimeout(function() {
console.log(result.isPending());
}, 800);
setTimeout(function() {
console.log(result.isPending());
console.log(result.reason());
}, 1500);
}, 2000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment