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 :: Uncaught TypeError: $(...).datatables is not a function 
Javascript :: vue v-for loop array 
Javascript :: jquery traversing methods 
Javascript :: Map put() method 
Javascript :: react input text onchange target method 
Javascript :: != javascript 
Javascript :: vue send data between components 
Javascript :: filtering jquery 
Javascript :: check if jwt token is valid 
Javascript :: run function after another function javascript 
Javascript :: google pay in react js 
Javascript :: javascript split multiple values 
Javascript :: web scraping using javascript 
Javascript :: shorthand arrow function 
Javascript :: loopback 
Javascript :: mock javascript function 
Javascript :: javascript if one line 
Javascript :: vanilla tilt js 
Javascript :: base64 to base64url javascript 
Javascript :: bind in javascript example 
Javascript :: datatable hide no data available in table 
Javascript :: jsonl parser javascript 
Javascript :: replace spaces with dashes 
Javascript :: JS how to access a class propert 
Javascript :: anonymous function parameters javascript 
Javascript :: spread operator in js 
Javascript :: javascript dom methods 
Javascript :: js filter array 
Javascript :: --env production 
Javascript :: base64 
ADD CONTENT
Topic
Content
Source link
Name
2+8 =