Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

how to get promise state in js

// define the following function to get the PromiseState
function promiseState(p) {
  const t = {};
  return Promise.race([p, t])
    .then(v => (v === t)? "pending" : "fulfilled", () => "rejected");
}

// create 3 test Promise examples:
var a = Promise.resolve();
var b = Promise.reject();
var c = new Promise(() => {});

// demonstrated value for each Promise example
promiseState(a).then(state => console.log(state)); // fulfilled
promiseState(b).then(state => console.log(state)); // rejected
promiseState(c).then(state => console.log(state)); // pending
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #promise #state #js
ADD COMMENT
Topic
Name
2+5 =