Function generate

  • Create compound task from generator function

    Applying yield to task within the generator function awaits the task and returns underlying value in case of success However the convinient option for typescript is to use yield* task.generator() as othervise one may have to deal with union types

    In case of failure an error is thrown and may be caught by try-catch

    If error is not caught the task is interrupted and returns 'just left error' immediately. In case of cancelation the task is interrupted and returns 'nothing' immediately

    Type Parameters

    • T

    • TT extends Task<T>

      yielded task type

    • R

      returned task resolve type

    Parameters

    Returns Task<R>

    task resolving to generator's return type

    Note

    compound task execution is interrupted only at yield statements, so despite returning immediately any promise-based chains would continue running until the first yield

    Note

    due to type unpredictability you HAVE to use .generator() together with yield* to avoid type issues, despite the fact that task may be yielded directly

    Example

    const task = Task.generate(function*() {
    try {
    const value1 = yield* delayedValueTask('data', 400).generator();

    const value2 = yield* delayedValueTask(value1.length, 300).generator();

    return value2;
    } catch (e) {
    return -1;
    }
    });

Generated using TypeDoc