DEV Community

BekmuhammadDev
BekmuhammadDev

Posted on

1 1 1 1 1

Javascript Promis

Image description

JavaScript promises asinxron operatsiyalarni boshqarishning qulay usullaridan biri hisoblanadi. promises kelajakda bajarilishi yoki muvaffaqiyatsiz tugashi mumkin bo'lgan qiymatlarni ifodalaydi. Ular asinxron operatsiyalarning natijalarini boshqarish uchun ishlatiladi va callback funksiyalar bilan bog'liq muammolarni kamaytiradi.

promis table

Image description

promises yaratish

JavaScript-da promis yaratish uchun Promise konstruktoridan foydalaniladi.

let myPromise = new Promise((resolve, reject) => {
    let success = true; // Bu yerda sizning asinxron operatsiyangiz bo'lishi mumkin

    if (success) {
        resolve("Bu operatsiya muvaffaqiyatli tugadi!");
    } else {
        reject("Bu operatsiya muvaffaqiyatsiz tugadi.");
    }
});

Enter fullscreen mode Exit fullscreen mode
  • resolve
  • reaject

resolve va reject funksiyalari JavaScript promises asinxron operatsiyalarning natijasini boshqarish uchun ishlatiladi.

resolve

resolve funksiyasi promis muvaffaqiyatli bajarilganda chaqiriladi. Bu funksiya o'ziga argument sifatida biror qiymat oladi, va bu qiymat keyinchalik .then() metodiga uzatiladi.

let Promise = new Promise((resolve, reject) => {
    let success = true; // Bu yerda sizning asinxron operatsiyangiz bo'lishi mumkin

    if (success) {
        resolve("Bu operatsiya muvaffaqiyatli tugadi!");
    }
});

Enter fullscreen mode Exit fullscreen mode

Yuqoridagi misolda, success o'zgaruvchisi true bo'lsa, resolve funksiyasi chaqiriladi va "Bu operatsiya muvaffaqiyatli tugadi!" qiymatini uzatadi.

then metodi orqali cansole.log da natijani chiqarish:

Promise.then((result) => {
    console.log(result); // "Bu operatsiya muvaffaqiyatli tugadi!" ni cansole.log da chiqaradi
});

Enter fullscreen mode Exit fullscreen mode

reject

reject funksiyasi promis muvaffaqiyatsiz tugaganda chaqiriladi. Bu funksiya o'ziga argument sifatida biror xatolik yoki muvaffaqiyatsizlik haqidagi ma'lumotni oladi, va bu qiymat keyinchalik .catch() metodiga uzatiladi.

let mPromise = new Promise((resolve, reject) => {
    let success = false; // Bu yerda sizning asinxron operatsiyangiz bo'lishi mumkin

    if (!success) {
        reject("Bu operatsiya muvaffaqiyatsiz tugadi.");
    }
});

Enter fullscreen mode Exit fullscreen mode

Yuqoridagi misolda, success o'zgaruvchisi false bo'lsa, reject funksiyasi chaqiriladi va "Bu operatsiya muvaffaqiyatsiz tugadi." qiymatini uzatadi.

mPromise
    .then((result) => {
        console.log(result); // Bu yerga kirmaydi keyingi blockga o'tadi
    })
    .catch((error) => {
        console.error(error); // "Bu operatsiya muvaffaqiyatsiz tugadi." ni cansole.logda chop  etadi
    });

Enter fullscreen mode Exit fullscreen mode

Image description

Sentry blog image

How I fixed 20 seconds of lag for every user in just 20 minutes.

Our AI agent was running 10-20 seconds slower than it should, impacting both our own developers and our early adopters. See how I used Sentry Profiling to fix it in record time.

Read more

Top comments (0)

Heroku

This site is powered by Heroku

Heroku was created by developers, for developers. Get started today and find out why Heroku has been the platform of choice for brands like DEV for over a decade.

Sign Up

👋 Kindness is contagious

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay