In some situations, for code execution speed performance, it is good to call async/await functions parallel or sequential for code optimization. Also to accomplish some features the executions should wait to complete execution of some codes or the execution should proceed by completing execution of some code parts one by one, in these cases it helps to call async/await functions parallel or sequential.

Process functions which returns Promise

To demonstrate call multiple async/await functions parallel, below is the function process which return promise after a specific time which are decided by calling function. This function is being called from few async functions on which parallel calling will be tested.

function process(val, sec, rej = false) {
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            if (!rej) {
                resolve(val);
            } else {
                reject('Error');
            }
        }, sec);
    });
}

Async functions

Below are the async functions which will call process function with the parameter values which should be solved from promise and time after which ms the promise needs to be solved. Promise also has a optional variable for reject promise, which is not set here, so no call will return as reject.

async function process_one() {
    return process(10, 500);
}

async function process_two() {
    return process(40, 100);
}

async function process_three() {
    return process(50, 1000);
}

async function process_four() {
    return process(20, 800);
}

Call Multiple async/await Functions Parallel

Below is the run_processes function which helps to call all async functions parallel with try catch error handling .

async function run_processes() {
    let res = null;
    try {
        res = await Promise.all([
            process_one(),
            process_two(),
            process_three(),
            process_four()
        ]);
        console.log('Success >>', res);
    } catch (err) {
        console.log('Fail >>', res, err);
    }
}

Let’s call run_processes.

run_processes();

Now the whole file will be look like,

function process(val, sec, rej = false) {
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            if (!rej) {
                resolve(val);
            } else {
                reject('Error');
            }
        }, sec);
    });
}

async function process_one() {
    return process(10, 500);
}

async function process_two() {
    return process(40, 100);
}

async function process_three() {
    return process(50, 1000);
}

async function process_four() {
    return process(20, 800);
}

async function run_processes() {
    let res = null;
    try {
        res = await Promise.all([
            process_one(),
            process_two(),
            process_three(),
            process_four()
        ]);
        console.log('Success >>', res);
    } catch (err) {
        console.log('Fail >>', res, err);
    }
}

run_processes();

Output Result Snapshot

Result of Call Multiple async/await Functions Parallel without Error

Test Error on Promise with Parallel Call

To make promise return error, let’s pass third argument to process functions with value true from functions process_three.

async function process_three() {
    return process(50, 1000, true);
}

And here is the full code of file after changing process_three for return error (reject promise) from process functions.

function process(val, sec, rej = false) {
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            if (!rej) {
                resolve(val);
            } else {
                reject('Error');
            }
        }, sec);
    });
}

async function process_one() {
    return process(10, 500);
}

async function process_two() {
    return process(40, 100);
}

async function process_three() {
    return process(50, 1000, true);
}

async function process_four() {
    return process(20, 800);
}

async function run_processes() {
    let res = null;
    try {
        res = await Promise.all([
            process_one(),
            process_two(),
            process_three(),
            process_four()
        ]);
        console.log('Success >>', res);
    } catch (err) {
        console.log('Fail >>', res, err);
    }
}

run_processes();

Output Result Snapshot

Result of Call Multiple async/await Functions Parallel with Error

Call Multiple async/await Functions Sequential

Calling multiple async/await functions sequential is same as call a async function with await keyword. To do this, let’s make some changes on run_processes functions so it can call all functions sequential but will results same output.

async function run_processes() {
    let res = null;
    try {
        res = [];
        res.push(await process_one());
        res.push(await process_two());
        res.push(await process_three());
        res.push(await process_four());
        console.log('Success >>', res);
    } catch (err) {
        console.log('Fail >>', res, err);
    }
}

Full code of file after doing changes on run_processes.

function process(val, sec, rej = false) {
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            if (!rej) {
                resolve(val);
            } else {
                reject('Error');
            }
        }, sec);
    });
}

async function process_one() {
    return process(10, 500);
}

async function process_two() {
    return process(40, 100);
}

async function process_three() {
    return process(50, 1000);
}

async function process_four() {
    return process(20, 800);
}

async function run_processes() {
    let res = null;
    try {
        res = [];
        res.push(await process_one());
        res.push(await process_two());
        res.push(await process_three());
        res.push(await process_four());
        console.log('Success >>', res);
    } catch (err) {
        console.log('Fail >>', res, err);
    }
}

run_processes();

Output Result Snapshot

Result of Call Multiple async/await Functions Sequential

And if you want to handle error here. You can use the same try catch for it, the file already has that try catch coded on it.