async / await in JavaScript | asynchronous operations | async functions | asinxron kod #kody_az
Dərs haqqında
async və await JavaScript-in ən zərif xüsusiyyətlərindən biridir, çünki onlar Promise-lərlə işləməyi həm güclü, həm də oxunaqlı hala gətirir. Hər dəfə async yazanda, sanki funksiyanı “gələcəkdə cavab verəcək” bir konteynerə çevirirsən — yəni o, avtomatik Promise qaytarır. await isə bu konteynerin cavabını gözləməyə imkan verir və nəticə gələnə qədər kodun o sətirdə dayanır, amma digər kodların icrasını bloklamır. Bu, “məni gözlə, amma dünyanı dayandırma” prinsipidir. Əgər await yazmasan, sən sadəcə Promise obyektini alarsan, amma onun içindəki dəyəri yox. try...catch bu sistemdə sanki Promise-in then və catch hissələrinin sadələşmiş versiyası kimidir — oxumaq və idarə etmək asan olur. Əgər bir neçə asinxron əməliyyat eyni vaxtda baş versin istəyirsənsə, Promise.all() onları paralel işlədərək nəticələri birlikdə qaytarır. async və await həmçinin Promise.race, Promise.any və Promise.allSettled kimi metodlarla da əla işləyir, çünki onlar hamısı eyni dilin — asinxron icranın — müxtəlif dialektləridir. Bu mexanizmlər bir yerdə kodu “gözləyən, amma donmayan” hala gətirir, yəni həm insan kimi düşünən, həm də kompüter kimi işləyən bir balans yaradır. Asinxronluq burada sadəcə texniki deyil, fəlsəfi bir anlayışdır — hadisələrin eyni vaxtda baş verə bilməsi və sükunət içində gözləməyin gücüdür.
====
async and await are among the most elegant features in JavaScript because they make working with Promises both powerful and readable. Every time you write async, you’re turning a function into a container that “will answer in the future” — it automatically returns a Promise. The await keyword lets you pause inside that container until the Promise gives its result, but it doesn’t freeze the rest of the program. It’s like saying, “wait for me, but don’t stop the world.” Without await, you only get the Promise object itself, not its resolved value. The try...catch structure works perfectly with await, acting as a cleaner and more natural version of .then() and .catch(). When you want several asynchronous tasks to happen at once, Promise.all() lets them run in parallel and returns all their results together. async and await also blend beautifully with Promise.race, Promise.any, and Promise.allSettled, since they’re all part of the same asynchronous ecosystem. Together, they make code that “waits but never freezes” — logical for humans and efficient for machines. Asynchrony in JavaScript isn’t just a technical idea; it’s almost philosophical — the art of letting things happen at their own pace while everything else keeps moving.
#kody_az #kodyaz #js #javascript #nodejs #es6 #viral #viralcode #nodejs #viralvideo #random #code #coder #azerbaycan #azerbaijan #baku #baki #programming #programmer #codes #hazırlıq #hazirliq #kurs #kurslar #course #courses #online #onlinecourses #test #quiz