“bagaimana mendapatkan status janji di js” Kode Jawaban

janji menyatakan 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.
Mysterious Monkey

bagaimana mendapatkan status janji di 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
Anxious Alligator

Jawaban yang mirip dengan “bagaimana mendapatkan status janji di js”

Pertanyaan yang mirip dengan “bagaimana mendapatkan status janji di js”

Lebih banyak jawaban terkait untuk “bagaimana mendapatkan status janji di js” di JavaScript

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya