safeAwait() function
Execute a promise safely
Signature:
typescript
export declare function safeAwait<T, U = Error>(promise: Promise<T>, errorExt?: object): Promise<[U, undefined] | [null, T]>;Parameters
Parameter | Type | Description |
|---|---|---|
promise |
| |
errorExt | object | (Optional) Additional Information you can pass safeAwait the err object { Promise } |
Returns:
Promise<[U, undefined] | [null, T]>
Example
async function asyncTaskWithCb(cb) { let err, user, savedTask, notification;
[ err, user ] = await safeAwait(UserModel.findById(1)); if(!user) return cb('No user found');
[ err, savedTask ] = await safeAwait(TaskModel({userId: user.id, name: 'Demo Task'})); if(err) return cb('Error occurred while saving task')
cb(null, savedTask); }