Skip to content

Instantly share code, notes, and snippets.

@phenax
Last active September 28, 2021 06:44
Show Gist options
  • Save phenax/d3eb3de699bb64ad24466ee5a5c23bf6 to your computer and use it in GitHub Desktop.
Save phenax/d3eb3de699bb64ad24466ee5a5c23bf6 to your computer and use it in GitHub Desktop.
Fold promise immedietely
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