Promises - Creating and Handling Promises

 Ballanqaadka (Promise) JavaScript waa shay matalaya guusha ama fashilka hawl asyncronous ah (hawl aan si toos ah loo maamulin). Waxa uu ka caawiyaa in laga fogaado "callback hell" oo uu xasiliyo koodhka asyncronous-ka ah, taasoo ka dhigaysa mid fudud oo la akhrin karo. 

Promises - Creating and Handling Promises
Promises - Creating and Handling Promises
Waa maxay Promises?

Promise waa object JavaScript ah oo matalaya hawl la sugayo inay fulanto mustaqbalka. Waxaa loo isticmaalaa in lagu maareeyo hawlaha asyncronous-ka ah si looga fogaado dhibaatada callback hell, taas oo ah marka la isticmaalo functions badan oo isku xiran (nested callbacks), taasoo keenaysa in koodhku adkaado in la fahmo.

Promise wuxuu leeyahay saddex xaaladood oo kala ah:

  1. Pending – Marka la abuuro, wali ma fulmin mana fashilmin.
  2. Fulfilled – Marka hawshii la sugayay ay guulaysato.
  3. Rejected – Marka hawshii la sugayay ay fashilanto.

Sida loo abuuro Promise

Promise waxaa la abuurayaa adigoo adeegsanaya constructor-ka Promise(). Waxay qaadataa hal function oo leh laba parameters:

  • resolve – Waa function la waco haddii hawshu ay guulaysato.
  • reject – Waa function la waco haddii hawshu ay fashilanto.

Tusaale ahaan, aan abuurno promise fudud oo simulating ah hawl waqti qaadanaysa:

Sida loo maareeyo Promise

Si aad u maareyso Promise, waxaa loo adeegsadaa .then() iyo .catch().

  • .then() – Waxaa lagu maareeyaa natiijada (fulfilled) ee Promise-ka.
  • .catch() – Waxaa lagu qabtaa qaladaadka haddii Promise-ka uu reject noqdo.

Tusaale:

javascript

myPromise .then((result) => { console.log(result); // Haddii Promise-gu uu guuleysto }) .catch((error) => { console.log(error); // Haddii uu fashilmo });
Promises - Creating and Handling Promises
Promises - Creating and Handling Promises

Sida loo isticmaalo multiple Promises

Mararka qaar, waxaad rabtaa inaad la tacaasho dhowr Promise isku mar. Waxaa jira dhowr farsamo oo loo adeegsado sida:

  1. Promise.all() – Waxaa la sugayaa dhammaan promises inay dhamaadaan.
  2. Promise.race() – Waxaa la sugayaa promise-kii ugu horreeyay ee dhammaada, xitaa haddii ay tahay reject.
  3. Promise.allSettled() – Waxaa la sugayaa dhammaan promises si loo arko natiijo kasta.
  4. Promise.any() – Waxaa la sugayaa mid ka mid ah promises-ka oo guuleysta.

Tusaale:

javascript

let p1 = new Promise((resolve) => setTimeout(resolve, 1000, "Promise 1")); let p2 = new Promise((resolve) => setTimeout(resolve, 2000, "Promise 2")); let p3 = new Promise((resolve, reject) => setTimeout(reject, 1500, "Promise 3 Failed")); Promise.all([p1, p2, p3]) .then((values) => { console.log("Dhammaan way guuleysteen:", values); }) .catch((error) => { console.log("Mid kamid ah waa uu fashilmay:", error); });

Gabagabo

Promises waa hab aad muhiim u ah oo lagu maareeyo xogta asyncronous-ka ah ee JavaScript. Waxay yareeyaan dhibaatada callback hell waxayna fududeeyaan koodhka. Adeegsiga .then(), .catch(), iyo multiple Promises sida Promise.all() waxay kaa caawinayaan inaad si hufan ula tacaasho xogta.

Haddii aad rabto inaad kor u qaaddo xirfaddaada JavaScript, barashada Promises waa tallaabo muhiim ah!


Previous Post Next Post

Important

Important