Computer >> कंप्यूटर ट्यूटोरियल >  >> प्रोग्रामिंग >> JavaScript

JavaScript में async/await से फंक्शन्स को इटरेटिवली कैसे चलाएं?

async/await के साथ लूप का उपयोग करके एसिंक्रोनस फंक्शन्स को क्रमिक रूप से (एक के बाद एक) चलाएं।

बुनियादी पैटर्न

async function runSequentially(times) {
    for (let i = 1; i <= times; i++) {
        await asyncTask1(`कॉल ${i} - Task 1`);
        await asyncTask2(`कॉल ${i} - Task 2`);
    }
}

async function asyncTask1(label) {
    return new Promise(resolve => {
        setTimeout(() => {
            console.log(label);
            resolve();
        }, 100); // सिम्युलेटेड एसिंक काम
    });
}

async function asyncTask2(label) {
    return new Promise(resolve => {
        setTimeout(() => {
            console.log(label);
            resolve();
        }, 100);
    });
}

// चलाएं
runSequentially(3);

आउटपुट

कॉल 1 - Task 1
कॉल 1 - Task 2
कॉल 2 - Task 1
कॉल 2 - Task 2
कॉल 3 - Task 1
कॉल 3 - Task 2

ऐरे के फंक्शन्स पर इटरेट करें

const asyncFunctions = [
    () => delay(100).then(() => console.log('Task 1')),
    () => delay(100).then(() => console.log('Task 2')),
    () => delay(100).then(() => console.log('Task 3'))
];

async function runAllSequentially(functions) {
    for (const fn of functions) {
        await fn(); // प्रत्येक पूरा होने का इंतजार करें
    }
    console.log('सभी पूरे हुए');
}

function delay(ms) {
    return new Promise(resolve => setTimeout(resolve, ms));
}

runAllSequentially(asyncFunctions);

पैरालेल (समानांतर) बनाम सीक्वेंशियल

// ❌ सीक्वेंशियल - धीमा (कुल समय = योग)
for (const fn of functions) {
    await fn();
}

// ✅ पैरालेल - तेज़ (कुल समय = अधिकतम)
await Promise.all(functions.map(fn => fn()));

// ✅ बैच्ड - बैच में समानांतर
async function runInBatches(functions, batchSize = 2) {
    for (let i = 0; i < functions.length; i += batchSize) {
        const batch = functions.slice(i, i + batchSize);
        await Promise.all(batch.map(fn => fn()));
    }
}

रिटर्न वैल्यूज़ के साथ

async function fetchUserData(userIds) {
    const results = [];
    
    for (const id of userIds) {
        const data = await fetch(`/api/user/${id}`).then(r => r.json());
        results.push(data);
        console.log(`यूजर ${id} लोडेड`);
    }
    
    return results;
}

// या map के साथ (यदि क्रम महत्वपूर्ण नहीं)
async function fetchAllParallel(userIds) {
    const promises = userIds.map(id => 
        fetch(`/api/user/${id}`).then(r => r.json())
    );
    return Promise.all(promises);
}

चलाने का तरीका

node async-iteration.js

मुख्य बिंदु

  • for...of लूप await के साथ सही काम करता है
  • forEach await के साथ काम नहीं करता (कॉलबैक async नहीं होता)
  • map + Promise.all = पैरालेल
  • for...of + await = सीक्वेंशियल
  1. जावास्क्रिप्ट call() मेथड आर्गुमेंट्स के साथ जावास्क्रिप्ट call() मेथड आर्गुमेंट्स के साथ

    परिचय जावास्क्रिप्ट call() मेथड हमें एक ही मेथड को अलग-अलग ऑब्जेक्ट्स के साथ उपयोग करने की अनुमति देता है। पैरामीटर्स को अलग-अलग पास किया जाता है (कॉमा-सेपरेटेड)। यह this को स्पष्ट रूप से सेट करता है। apply() समान है लेकिन आर्गुमेंट्स को ऐरे के रूप में लेता है। bind() एक नया फ़ंक्शन लौटाता है जिसमे

  1. जावास्क्रिप्ट में call() और apply() के साथ फ़ंक्शन्स को इनवोक करना जावास्क्रिप्ट में call() और apply() के साथ फ़ंक्शन्स को इनवोक करना

    जावास्क्रिप्ट में call() और apply() के साथ फ़ंक्शन्स को इनवोक करने का कोड निम्नलिखित है - उदाहरण <!DOCTYPE html> <html lang=en> <head> <meta charset=UTF-8 /> <meta name=viewport content=width=device-width, initial-scale=1.0 /> <title>Document</title> <s

  1. जावास्क्रिप्ट में  onclick  का उपयोग करके दो या अधिक फ़ंक्शन कैसे चलाएं? जावास्क्रिप्ट में 'onclick' का उपयोग करके दो या अधिक फ़ंक्शन कैसे चलाएं?

    आइए पहले एक बटन सेट करें - Call ऊपर, हमने onclick के तहत एक फ़ंक्शन सेट किया है जो दो अन्य फ़ंक्शन्स को कॉल करता है -function callTwoOtherFunctions(){ fun1(); fun2(); }पूरे कोड में नीचे दिखाए अनुसार fun1() और fun2() के साथ काम करें -उदाहरण  Document Call function callTwoOtherFunctio