Skip to content

Instantly share code, notes, and snippets.

@pibi
Created March 13, 2019 10:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pibi/79defe97e4e4a5844cd1aedace7daac6 to your computer and use it in GitHub Desktop.
Save pibi/79defe97e4e4a5844cd1aedace7daac6 to your computer and use it in GitHub Desktop.
// let's see if the same happens using a common resolved promise
let promised = 42.1
// here we are promisify-ing the thenable
const staticPromise = (async () => {
return ++promised
})()
const asyncStatic = async () => {
return staticPromise
}
const asyncAwaitStatic = async () => {
return await staticPromise
}
const asyncAsyncStatic = async () => {
return asyncStatic()
}
console.log(staticPromise)
// expected to be true, they are ALL false
console.log(staticPromise === asyncStatic(), asyncStatic() === asyncStatic(), asyncStatic() === asyncAwaitStatic(), asyncStatic() === asyncAsyncStatic())
// this is the first call to then
console.log('staticPromise', staticPromise.then(console.log.bind(console, 'staticPromise')))
console.log('asyncStatic', asyncStatic().then(console.log.bind(console, 'asyncStatic')))
console.log('asyncAwaitStatic', asyncAwaitStatic().then(console.log.bind(console, 'asyncAwaitStatic')))
console.log('asyncAsyncStatic', asyncAsyncStatic().then(console.log.bind(console, 'asyncAsyncStatic')))
let called = 0
let started
// this is a thenable
const start = () => ({
then (resolve, reject) {
return resolve(++called) || 'resolved'
}
})
// here we are promisify-ing the thenable
const asyncStart = async () => {
return started
}
const asyncAwaitStart = async () => {
return await started
}
const asyncAsyncStart = async () => {
return asyncStart()
}
// expected functions here
console.log(start)
console.log(asyncStart)
console.log(asyncAsyncStart)
console.log(asyncAwaitStart)
// initialize the promise
started = start()
// assertion and promises
console.log(started)
// I'm expecting the second to be true, but it isn't
console.log(asyncStart === asyncStart, asyncStart() === asyncStart())
// I'm expecting this to be true, as we are wrapping the same thenable, but it isn't
console.log(asyncStart() === asyncAsyncStart())
// this is the first call to then
console.log('start', started.then(console.log.bind(console, 'start')))
// things is getting weird here: the started thenable is called everytime, meaning it is in common to everyone
console.log('asyncStart', asyncStart().then(console.log.bind(console, 'asyncStart')))
console.log('asyncAwaitStart', asyncAwaitStart().then(console.log.bind(console, 'asyncAwaitStart')))
console.log('asyncAsyncStart', asyncAsyncStart().then(console.log.bind(console, 'asyncAsyncStart')))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment