Subjects

All subjects Django Java Python React Spring Boot JavaScript PHP
Sign Up Free
Interview question

How does async/await interact with the event loop compared to raw Promises? async/await raw Promises की तुलना में event loop के साथ कैसे interact करता है?

Answer

async/await doesn't change how the event loop fundamentally works - it's still built on Promises and microtasks. However, await pauses the async function's execution at that point, letting other code run in the meantime.

console.log('1: Start');

async function asyncFunc() {
    console.log('2: Inside async function');
    await null; // even awaiting a non-promise value schedules a microtask
    console.log('4: After await'); // this runs as a MICROTASK
}

asyncFunc();

console.log('3: After calling asyncFunc');

// Output: 1: Start, 2: Inside async function, 3: After calling asyncFunc, 4: After await
// Explanation: code BEFORE the first await runs synchronously.
// The await pauses the function and schedules the REST of it as a microtask,
// allowing the calling code (line 3) to continue first

// Equivalent using raw Promises to show they behave the same way
function promiseFunc() {
    console.log('2: Inside promise function');
    return Promise.resolve(null).then(() => {
        console.log('4: After then'); // microtask, same timing as await
    });
}
promiseFunc();
console.log('3: After calling promiseFunc');

// Multiple awaits each create a new microtask checkpoint
async function multiStep() {
    console.log('A');
    await null;
    console.log('B'); // microtask 1
    await null;
    console.log('C'); // microtask 2, queued after B resolves
}
multiStep();
console.log('D');
// Output: A, D, B, C - each await yields control back to the event loop once

async/await event loop के fundamental काम करने के तरीके को नहीं बदलता - यह अभी भी Promises और microtasks पर बना है। पर await उस point पर async function के execution को pause करता है, बाकी code को चलने देता है।

console.log('1: Start');

async function asyncFunc() {
    console.log('2: Inside async function');
    await null; // गैर-promise value await करना भी microtask schedule करता है
    console.log('4: After await'); // यह MICROTASK की तरह चलता है
}

asyncFunc();

console.log('3: After calling asyncFunc');

// Output: 1: Start, 2: Inside async function, 3: After calling asyncFunc, 4: After await
// पहले await से पहले का code sync चलता है।
// await function को pause करता है और बाकी को microtask बना देता है

// Raw Promises से equivalent - same व्यवहार दिखाने के लिए
function promiseFunc() {
    console.log('2: Inside promise function');
    return Promise.resolve(null).then(() => {
        console.log('4: After then'); // microtask, await जैसी timing
    });
}
promiseFunc();
console.log('3: After calling promiseFunc');

// Multiple awaits हर एक नया microtask checkpoint बनाते हैं
async function multiStep() {
    console.log('A');
    await null;
    console.log('B');
    await null;
    console.log('C');
}
multiStep();
console.log('D');
// Output: A, D, B, C

Was this answer clear?