Skip to content

Instantly share code, notes, and snippets.

@wearhere
Last active July 31, 2020 01:25
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wearhere/48510f94ac1f4be18d5a17b50c96fe6a to your computer and use it in GitHub Desktop.
Save wearhere/48510f94ac1f4be18d5a17b50c96fe6a to your computer and use it in GitHub Desktop.
Using async/await with Bluebird in Node 7.6.0.
/* eslint no-console: false */
const bluebird = require('bluebird');
function isBluebirdPromise(promise) {
return promise.constructor === bluebird;
}
function respond() {
return new bluebird((resolve) => {
setTimeout(() => resolve('hi'), 2000);
});
}
async function respondAsync() {
return respond();
}
async function intermediate() {
let promise = respond();
console.log('`respond` returns Bluebird promise:', isBluebirdPromise(promise));
return await promise;
}
async function main() {
console.log('can you await bluebird promises?', await respond() === 'hi');
console.log('will async avoid double-wrapping bluebird promises?', await respondAsync() === 'hi');
console.log('but look at the mixed types in the following logs');
let promise = intermediate();
console.log('`intermediate` returns Bluebird promise:', isBluebirdPromise(promise));
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment