How can I reject a promise that returned by an async/await function?
e.g. Originally:
I have a suggestion to properly handle rejects in a novel approach, without having multiple try-catch blocks.
import to from './to';
async foo(id: string): Promise {
let err, result;
[err, result] = await to(someAsyncPromise()); // notice the to() here
if (err) {
return 400;
}
return 200;
}
Where the to.ts function should be imported from:
export default function to(promise: Promise): Promise {
return promise.then(data => {
return [null, data];
}).catch(err => [err]);
}
Credits go to Dima Grossman in the following link.