External module "promise/try-catch"
Functions
tryCatch
- tryCatch<T, E>(promise: Promise<T>, defaultResult?: T): Promise<[E, T]>
-
Type parameters
Parameters
-
promise: Promise<T>
-
Default value defaultResult: T = null
Returns Promise<[E, T]>
Error and result array
Try Catch
Merges result and error in the same callback
Example:
import { tryCatch } from "@thalesrc/js-utils/promise"; async function fooFunction() { const promise = anAsyncCall(); const [error, result] = await tryCatch(promise); if (error) { // handle error } // do stuff }
Prototype Example:
import "@thalesrc/js-utils/promise/proto/try-catch"; async function fooFunction() { const [error, result] = await anAsyncCall().tryCatch(); if (error) { // handle error } // do stuff }