Last active
September 28, 2021 06:44
-
-
Save phenax/d3eb3de699bb64ad24466ee5a5c23bf6 to your computer and use it in GitHub Desktop.
Fold promise immedietely
This file contains 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
const PENDING_STATE = Symbol.for('MyPromise.PENDING'); | |
const foldPromiseImmedietely = (promise, { RESOLVED, PENDING, REJECTED }) => | |
Promise.race([ promise, Promise.resolve(PENDING_STATE) ]) | |
.then(x => x === PENDING_STATE ? PENDING() : RESOLVED(x)) | |
.catch(REJECTED); | |
const message = await foldPromiseImmedietely(teraPromise, { | |
RESOLVED: value => `Got ma ${value}`, | |
PENDING: () => 'Waitin for ma value', | |
REJECTED: e => 'Got an error boey', | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment