Function thenableFactory

  • After I created the thenable function, my code became easier, because I could write the same code regardless whether the input was synchronous or asynchronous. But by wrapping something with thenable, the check whether it was a Promise or not was done on every invocation.

    In a library that is about iterators, we expect this to be called many times. So it feels like it could make sense to create a version that 'remembers' the conclusions from the first run, and that will use that knowledge in the second run (assuming that every next element in an iterator will be a promise if the first was a promise and vice versa)

    A few tests seemed to indicate that calling isPromise often if about 10x slower than checking if a variable is true or false (or is a specific symbol), so there should be gain to be made with this.

    Example

    // instead of
    for (x of [1, 2, 3]) {
    thenable(x).then((v) => console.log(v));
    }
    // do something like
    const cachedThenable = thenableFactory(1);
    for (x of [1, 2, 3]) {
    cachedThenable(x).then((v) => console.log(v))
    }

    Returns

    a thenable(...)-like function that has assumptions built-in based on the first x

    Type Parameters

    • T

    Parameters

    • y: T | Promise<T>

    Returns ((x: T | Promise<T>) => TThenable<T>)

Generated using TypeDoc