Function useLock

  • Promise-based lock implementation.

    When someone tries to acquire a lock using acquire they receive a promise for a release callback that is fulfilled as soon as previous lock owner invokes their release callback. If acquire is called after unmount then the returned promise is never fulfilled.

    Returns [isLocked: boolean, acquire: (() => Promise<(() => void)>)]

    const [locked, acquire] = useLock();

    async function doSomething() {
    const release = await acquire();
    try {
    // Long process starts here
    } finally {
    release();
    }
    }

    // Long process would be executed three times sequentially
    doSomething();
    doSomething();
    doSomething();