a-promise-wrapper v1.1.3
A Promise Wrapper
This utility object helps you avoid the try and catch blocks inside async functions. With no dependencies
As a tip: You could use object destructuring to get a well named rejection / resolved data back.
Index
Problem description
Normally you would write code similar to this using the async/await keywords
async function singleAsyncFunctionCall() {
try {
const response = await request.get("https://reqres.in/api/users/");
// Do something
} catch (e) {
// React to the error
}
}
If the function required to perform multiple async functions. Not that this would be a good practice because you might want to separate them into different functions, but you could end up having something like this:
async function multipleAsyncFunctionCalls() {
try {
const response = await request.get("https://reqres.in/api/users/");
// Do something
const hashedPassword = await crypto.hash("password", 10);
// Do something else
const dbResponse = await db.save({ any : object });
// Do something else
} catch (e) {
// You might need to check error properties to know what happened
if (e.hasOwnProperty("response") && e.hasOwnProperty("data")) {
// Catch request error
} else if (e.message.includes("hash")) {
// Catch hashing error
} else if (e.hasOwnProperty("type") && e.type == "DB Error") {
// Catch database error
} else {
// Uknown error
}
}
}
Or like this:
async function multipleAsyncFunctionCalls() {
try {
const response = await request.get("https://reqres.in/api/users/");
// Do something
} catch (e) {
// Catch the request error
}
try {
const hashedPassword = await crypto.hash("password", 10);
// Do something
} catch (e) {
// Catch hashing error
}
try {
const dbReponse = await db.save({ any : object });
// Do something
} catch (e) {
// Catch database error
}
}
Installation and usage
Install with npm
$ npm install a-promise-wrapper --save
Require it where you need to use it
const promiseWrapper = require("a-promise-wrapper");
// Or
import promiseWrapper from 'a-promise-wrapper';
The Promise Wrapper can help you clean up and reduce some of that code to improve readability.
Call the function passing in a function that returns a Promise.
The Promise Wrapper must be used inside a function that uses the async / await keywords
const promiseWrapper = require("a-promise-wrapper");
async function singleAsyncFunctionCall() {
const { data , error } = await promiseWrapper(request.get("https://reqres.in/api/users/"));
if (error) {
// Handle the error
}
// Do something
}
The return value if resolved will be:
{
data, // This holds the resolved value of the original promise
error : null
}
The return value if rejected will be:
{
data : null,
error // This holds the rejected value of the original promise
}
Examples
If you don't need one of the two objects you can just not use them
async function singleAsyncFunctionCall() {
// The error will be silenced
const { data } = await promiseWrapper(axios.get("https://reqres.in/api/users/"));
// Do something
}
// If you just care about the promise being rejected
async function singleAsyncFunctionCall() {
const { error } = await promiseWrapper(axios.get("https://reqres.in/api/users/"));
if (error) {
// Handle the error
}
}
You can use object destructuring to assign better names
async function singleAsyncFunctionCall() {
const { data : { data : userInfo } , error : errorFetghingUser } = await promiseWrapper(axios.get("https://reqres.in/api/users/"));
if (errorFetghingUser) {
// Handle the error
}
// Do something
console.log(userInfo);
}
It really shines with multiple async calls. Your code is way more readable
async function multipleAsyncFunctionCalls() {
const { data : userResponse, error : errorFetchingUser } = await promiseWrapper(request.get("https://reqres.in/api/users"));
if (errorFetchingUser) {
// Do something
}
const { data : hashedPassword, error : errorHashingPassword } = await promiseWrapper(crypto.hash("password", 10));
if (errorHashingPassword) {
// Do something
}
const { data : newObject, error : errorSavingObject } = await promiseWrapper(db.save({ any : object }));
if (errorSavingObject) {
// Do something
}
}
Multiple Promises
If you need to execute multiple promises in parallel you can use Promise.all or you can pass an array of promises to the wrapper
async function parallelAsyncFunctionCalls() {
const { data, error } = await promiseWrapper(Promise.all([
request.get("https://reqres.in/api/users/1"),
request.get("https://reqres.in/api/users/2"),
]));
const [response1, response2] = data;
// This would also work and will give you the same result
const { data, error } = await promiseWrapper([
request.get("https://reqres.in/api/users/1"),
request.get("https://reqres.in/api/users/2"),
]);
const [response1, response2] = data;
}