core3-utils

@wixc3/common on Github

Home > @wixc3/common > enforceSequentialExecution

enforceSequentialExecution() function

Ensures that when the async function fn is called twice in a row, the second call only begins after the first one has finished (successfully or not).

Signature:

export declare function enforceSequentialExecution<P, T extends (...args: any[]) => Promise<Awaited<P>>>(fn: T): T;

Parameters

Parameter Type Description
fn T

Returns:

T

Example

    const doWork = enforceSequentialExecution(() => {
        console.log('start');
        await sleep(1000);
        console.log('end');
    });
    void doWork();
    void doWork();
    // Result: start, end, start, end