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.

Revisions

  1. phenax created this gist Nov 17, 2018.
    15 changes: 15 additions & 0 deletions get-promise-status.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,15 @@

    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' }