Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

promise states javascript

// A JavaScript Promise can be in several states:

// - Pending - response is not ready yet. Please wait.
// - Fulfilled - response is ready. Success. Take the data.
// - Rejected - an error occurred. Handle it.
Comment

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
Comment

javascript promise state

// Check the state of a Promise

function yet(promise) {
    const yettable = {
        state: "pending",
        isPending() { return promise.state == "pending"; },
        isFulfilled() { return promise.state == "fulfilled"; },
        isRejected() { return promise.state == "rejected"; },
    };

    Object.assign(promise, yettable);

    promise
        .then(result => (promise.state = "fulfilled", result))
        .catch(err => (promise.state = "rejected", err));

    return promise;
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: angularjs make post request 
Javascript :: d3 v6 
Javascript :: for in loop javascript 
Javascript :: address format json 
Javascript :: flat function javascript 
Javascript :: datatable add filter dropdown 
Javascript :: next js get gurrent page params 
Javascript :: js get path from url 
Javascript :: react-native-bouncy-checkbox 
Javascript :: express mysql sessions 
Javascript :: sequelize get data 
Javascript :: next js link 
Javascript :: string length js 
Javascript :: object literal js 
Javascript :: npm md to html 
Javascript :: format phone number javascript 
Javascript :: javascript export 
Javascript :: unregister react hook form 
Javascript :: e.target.value to image url in javascript 
Javascript :: setting live reload sublime text 3 
Javascript :: floor html 
Javascript :: convert number to hex js 
Javascript :: absolute price in javascript 
Javascript :: Rounding off to desired no of digit after decimal 
Javascript :: create relationship between schema in sanity 
Javascript :: filter in javascipt 
Javascript :: how to transform object in string with scaped 
Javascript :: js min number in array 
Javascript :: javascript close calendar after select 
Javascript :: javascript link to google maps route 
ADD CONTENT
Topic
Content
Source link
Name
7+9 =