To-await
To-await is an efficient and clean way to write Asynchronous code as Synchronous code.
It it based on this article - How to write async await without try-catch blocks in Javascript it also has the only addition that we support Promise.All.
I have been using this technique for more than a year in production. It is a pretty simple approach to avoid callback hell and promise chains. Furthermore, I found my self nesting try and catch. These are the primary cases where this package alleviates issues.
Installation
npm install to-await
This assume that you are using npm as package manager.
The Gist
ES6 modules
;
CommonJS
const to toAll = ;
Promise Example
const firstPassTest = async { try let error result = await ; console; error result = await ; console; error = await ; console; if error throw error; catch err console; }; ;
Array of Promisses
const to toAll = ; const secondTest = async { const arrayOfPromises = array; const promises = ; let error data = await ; console;} ;
Where it might help you
In this case below, if you want to use a try catch to wrap you function, you are not able to throw an error inside of the callback/Promise since you are inside of another scope. This is an example from nodejs docs.
const fs = ;const promifisy = ; const writeAsync = ;const data = Buffer; { try fs; catch err console; } { try const error = await ; if error throw error; console; catch err}