Skip to content

Instantly share code, notes, and snippets.

@mishelashala
Last active July 27, 2020 13:44
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 mishelashala/2aabf17537ebf2a8f71051a158aa3bdd to your computer and use it in GitHub Desktop.
Save mishelashala/2aabf17537ebf2a8f71051a158aa3bdd to your computer and use it in GitHub Desktop.
// Now this a thunk.
// Yeah, computer science b*tch!
// PromisedNumber :: () -> Promise<number>
const PromisedNumber = () => new Promise(function (fulfill, reject) {
setTimeout(() => { fulfill(10) }, 500)
})
// PromisedString :: () -> Promise<string>
const PromisedString = () => new Promise(function (fulfill, reject) {
setTimeout(() => { fulfill(10) }, 1000)
})
console.log('start')
const AsyncPipeline = () => {
// we don't actually need these two variables
const promisedNumber = PromisedNumber()
const promisedString = PromisedString()
promisedNumber.then(
// using arrow functions to "save" some visual space
(value) => {
console.log('number: ', value)
promisedString.then(
(value) => { console.log('string:', value) },
(err) => { console.log('error:', err.message) }
)
},
(err) => { console.log('error: ', err.message) }
)
}
console.log('finish')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment