Skip to content

Instantly share code, notes, and snippets.

View mishelashala's full-sized avatar

Michell Ayala mishelashala

  • Mérida, Yucatan
View GitHub Profile
// resolve :: a -> Promise a
// reject :: a -> Promise a
// of :: a -> AsyncMonad a
// then :: (a -> b) -> Promise b
// map :: (a -> b) -> AsyncMonad b
// then :: (a -> Promise b) -> Promise b
// flatMap :: (a -> AsyncMonad b) -> AsyncMonad b
// resolve :: a -> Promise<a>
// reject :: a -> Promise<a>
// of :: (a) -> AsyncMonad a
// flatMap :: (a -> AsyncMonad b) -> AsyncMonad b
// map :: (a -> b) -> AsyncMonad b
// then :: (a -> b) -> Promise b
// then :: (a -> Promise b) -> Promise b
// fulfilled promise
const promisedNumber = Promise.resolve(12)
promisedNumber.then(value => console.log('number:', value))
// rejected promise
const rejectedString = Promise.reject(new Error('could not produce string'))
rejectedString.catch(err => console.log('error:', err.message))
// PromisedNumber :: () -> Promise<error>
const PromisedNumber = () => new Promise(function (fulfill, reject) {
setTimeout(() => { reject(new Error('could not produce number')) }, 500)
})
// PromisedString :: () -> Promise<string>
const PromisedString = () => new Promise(function (fulfill, reject) {
setTimeout(() => { fulfill(10) }, 1000)
})
console.log('start')
// PromisedNumber :: () -> Promise<Error>
const PromisedNumber = () => new Promise(function (fulfill, reject) {
setTimeout(() => { reject(new Error('could not produce number')) }, 500)
})
// 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')