Lesson 15 / الدرس 15

Why asynchronous code exists / لماذا وُجدت الشيفرة اللاتزامنية

A page has one thread, and everything competes for it: your code, the clicks, the scrolling, the drawing. Understanding that single queue is what makes promises and async/await make sense rather than feel like ceremony.

للصفحة خيط واحد، وكل شيء يتنافس عليه: شيفرتك والنقرات والتمرير والرسم. وفهم ذلك الطابور الواحد هو ما يجعل الوعود وasync/await منطقية بدل أن تبدو طقسًا.

One thread, one queue

while (Date.now() - start < 3000) { }   three seconds of work

during those three seconds the page cannot:
  redraw       respond to a click       accept typing       scroll smoothly
The demonstration runs a counter every 100 milliseconds and gives you a button that does three seconds of pointless arithmetic. Watch the counter while you hold the button down, and try typing in the box at the same time. Nothing is broken — the thread is simply busy, and a busy thread cannot do anything else.

What waiting actually means

Now replace the pointless arithmetic with something real: asking a server for data. That takes somewhere between 50 milliseconds and several seconds, and none of it is your code running — it is your code waiting. If waiting blocked the thread the way the first button did, every page that loads anything would freeze while it loaded. So the browser does not let you wait. It takes your request, hands you a receipt, and calls you back when the answer arrives. That receipt is a promise, and the next three lessons are about it.

Kind of workWho does the waitingBlocks the page?
A loop over a million numbersYour code — it is really runningYes
A network requestThe browser, elsewhereNo
A timerThe browserNo
Reading a file the user pickedThe browserNo
A very large JSON.parseYour codeYes

Asynchronous code helps with the middle three and does nothing at all for the first and last. This distinction matters: people reach for async to fix a slow loop and are surprised when it changes nothing. The fix for genuinely slow code is less work or smaller slices, which is what the second button demonstrated.

Try it live / جرّب بنفسك

Preview / المعاينة

Check yourself / اختبر نفسك

1. What happens to clicks made while the thread is blocked?

2. Will making a slow loop async stop it freezing the page?

3. What does "asynchronous" actually change?

Your task / مهمتك

Reproduce the freeze yourself: a visible heartbeat, a button that blocks for two seconds, and a text input. While it is blocked, click the button three more times and type a word, then describe in a comment exactly what happened when the thread came back. Then add a second button that does the same amount of work in slices and keeps the heartbeat running, and write one sentence on which of the two kinds of work in the table asynchronous code cannot help with.

أعد إنتاج التجمّد بنفسك: نبضة مرئية، وزر يحجب ثانيتين، ومدخل نصي. وأثناء الحجب انقر الزر ثلاث مرات أخرى واكتب كلمة، ثم صف في تعليق ما حدث بالضبط حين عاد الخيط. ثم أضف زرًا ثانيًا يقوم بالقدر نفسه من العمل في شرائح ويبقي النبضة تعمل، واكتب جملة عن أي نوعي العمل في الجدول لا تستطيع الشيفرة اللاتزامنية مساعدته.

  • A heartbeat that visibly stops نبضة تتوقف على نحو مرئي
  • Queued clicks and typing described النقرات المصفوفة والكتابة موصوفة
  • A sliced version that stays responsive نسخة مشرَّحة تبقى مستجيبة
  • One sentence on what async cannot fix جملة عما لا تصلحه اللاتزامنية
How do you want to submit? / كيف تريد التسليم؟