withResolvers()


ULASHISH

Tavsif

Promise.withResolvers() — Promise’larni yaratish va boshqarishni soddalashtiradigan statik metod.

Promise.withResolvers() Promise bilan bog‘liq resolve va reject funksiyalariga uning executor funksiyasidan tashqarida murojaat qilishning qulayroq usulini taqdim etadi.


Misol

const { promise, resolve, reject } = Promise.withResolvers(); // You can now use 'resolve' and 'reject' anywhere in your code // to control the state of 'promise'. // Simulate async work setTimeout(() => {   const success = Math.random() > 0.5;   if (success) {     resolve("Operation successful!");   } else {     reject("Operation failed!");   } }, 1000); promise   .then((message) => text = message))   .catch((error) => text = error);

An’anaviy new Promise((resolve, reject) => { ... }) konstruktor andozasi o‘rniga Promise.withResolvers() quyidagilarni o‘z ichiga olgan obyektni qaytaradi:

  • promise: Yangi yaratilgan Promise nusxasi
  • resolve: Promise’ni qiymat bilan bajarilgan holatga o‘tkazuvchi funksiya
  • reject: Promise’ni sabab (xato) bilan rad etuvchi funksiya

Sintaksis

Promise.withResolvers()

Parametrlar

YO‘Q

Qaytariladigan qiymat

Tip Tavsif
Object3 ta xususiyatni o‘z ichiga olgan obyekt:
1. Promise obyekti.
2. Promise’ni hal qiluvchi (resolve) funksiya.
3. Promise’ni rad etuvchi (reject) funksiya.



Brauzerlarda qo‘llab-quvvatlash

Promise.withResolvers() — JavaScript 2024 imkoniyati.

ES 2024 2024-yil martidan beri barcha zamonaviy brauzerlarda qo‘llab-quvvatlanadi:

Chrome
117
Edge
117
Firefox
119
Safari
17.4
Opera
103
2023-yil sentabr 2023-yil sentabr 2023-yil oktabr 2024-yil oktabr 2023-yil may

W3Schools Pathfinder

Yutuqlaringizni kuzating – bu bepul!