“Janji naskah allsettled” Kode Jawaban

Janji naskah allsettled

const myPromise = async (): Promise<string> => {
  return new Promise((resolve) => {
    resolve("hello world");
  });
};

const data = await Promise.allSettled([myPromise()]);

const response = (data.find(
  (res) => res.status === "fulfilled"
) as PromiseFulfilledResult<string> | undefined)?.value;

if (!response) {
  const error = (data.find(
    (res) => res.status === "rejected"
  ) as PromiseRejectedResult | undefined)?.reason;
  throw new Error(error);
}
Prickly Porcupine

naskah mendapatkan janji allsettled

/* To get this running on Linux, I needed the latest typescript version: */

npm install -g typescript@latest

/* Then in your tsconfig you currently need the ES2020.Promise lib. */

{
  "compilerOptions": {
    "lib": [
      "ES2020.Promise",
    ]
  },
}

/* Usage: */ const results = await Promise.allSettled(BatchOfPromises);
Cute Caterpillar

Jawaban yang mirip dengan “Janji naskah allsettled”

Pertanyaan yang mirip dengan “Janji naskah allsettled”

Lebih banyak jawaban terkait untuk “Janji naskah allsettled” di TypeScript

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya