Skip to content

Instantly share code, notes, and snippets.

@phenax
Created November 17, 2018 10:01
Show Gist options
  • Save phenax/bb7a1b0447f44e1024edfd6de89f91d5 to your computer and use it in GitHub Desktop.
Save phenax/bb7a1b0447f44e1024edfd6de89f91d5 to your computer and use it in GitHub Desktop.
Get the promise status
const promiseState = status => ({ status });
const PROMISE_STATE = {
PENDING: promiseState('PENDING'),
RESOLVED: promiseState('RESOLVED'),
REJECTED: promiseState('REJECTED'),
};
const getPromiseStatus = promise =>
Promise.race([ promise, Promise.resolve(PROMISE_STATE.PENDING) ])
.then(r => r === PROMISE_STATE.PENDING ? PROMISE_STATE.PENDING : PROMISE_STATE.RESOLVED)
.catch(e => PROMISE_STATE.REJECTED);
await getPromiseStatus(teraPromise); // >> { status: 'PENDING' }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment