Function asyncOperation

  • A better alternative to "new Promise()" that supports error handling and maintains the stack trace for Error.stack.

    When you use new Promise() you need to wrap your code inside a try-catch to call reject on error. asyncOperation() does this automatically.

    When you use new Promise() you will lose the stack trace when reject(new Error()) is called. asyncOperation() makes sure the error stack trace is the correct one.

    Example

    await asyncOperation(async (resolve, reject) => {
    await doSomething(); //if this fails, reject() will automatically be called
    stream.on('data', (data) => {
    resolve(data); //at some point you MUST call resolve(data)
    });
    });

    Type Parameters

    • T

    Parameters

    • executor: ((resolve: ((value: T) => void), reject: ((error: any) => void)) => void | Promise<void>)
        • (resolve: ((value: T) => void), reject: ((error: any) => void)): void | Promise<void>
        • Parameters

          • resolve: ((value: T) => void)
              • (value: T): void
              • Parameters

                • value: T

                Returns void

          • reject: ((error: any) => void)
              • (error: any): void
              • Parameters

                • error: any

                Returns void

          Returns void | Promise<void>

    Returns Promise<T>

Generated using TypeDoc