Safe Async Await
JS
S
JavaScriptTrivial implementation to handle errors on async await without the traditional try-catch statement
1// Safe Await
2// ====================== ./lib/safe-await ====================================
3export default function SafeAwait(promise: Promise<any>) {
4 return promise.then(data => {
5 return [null, data];
6 })
7 .catch(err => [err]);
8}
9
10// ====================== ./routes/fancy-route====================================
11import SafeAwait from '../classes/safe-await';
12const [error, url] = await SafeAwait(ResolverService.resolveImage(id));
13if (error) return next(error);
14// do your stuffCreated on 12/10/2018