Foundry Virtual Tabletop - API Documentation - Version 14
    Preparing search index...

    Class Semaphore

    A simple Semaphore implementation which provides a limited queue for ensuring proper concurrency.

    The maximum number of tasks which are allowed concurrently.

    // Some async function that takes time to execute
    function fn(x) {
    return new Promise(resolve => {
    setTimeout(() => {
    console.log(x);
    resolve(x);
    }, 1000);
    });
    }

    // Create a Semaphore and add many concurrent tasks
    const semaphore = new Semaphore(1);
    for ( let i of Array.fromRange(100) ) {
    semaphore.add(fn, i);
    }
    Index

    Properties

    Accessors

    Methods

    Properties

    max: number

    The maximum number of tasks which can be simultaneously attempted.

    Accessors

    • get active(): number

      The number of actively executing tasks

      Returns number

    • get remaining(): number

      The number of pending tasks remaining in the queue

      Returns number

    Methods

    • Add a new tasks to the managed queue

      Parameters

      • fn: Function

        A callable function

      • Optional...args: any[]

        Function arguments

      Returns Promise<any>

      A promise that resolves once the added function is executed

    • Abandon any tasks which have not yet concluded

      Returns void