Created
November 17, 2018 10:01
-
-
Save phenax/bb7a1b0447f44e1024edfd6de89f91d5 to your computer and use it in GitHub Desktop.
Get the promise status
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 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