Last active
July 31, 2020 01:25
-
-
Save wearhere/48510f94ac1f4be18d5a17b50c96fe6a to your computer and use it in GitHub Desktop.
Using async/await with Bluebird in Node 7.6.0.
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
/* 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