1.0.0 • Published 3 years ago

@jamarv4/seatbelt v1.0.0

Weekly downloads
-
License
MIT
Repository
-
Last release
3 years ago

Seatbelt - An async function wrapper

Description:

The seatbelt wrapper returns an array that contains an error as the first element and the resolved data as the second. The return result can then be destructured for a cleaner look without using a try/catch


// Usage:
// fn can be a promise or non-promise
const [err, data] = await seatbelt(fn);
// import
const seatbelt = require('@jamarv4/seatbelt');

// If the function returns successfully, 
// then the first element will be null (error), 
// and the second element will contain the data. -> [null, {user}]
const [error, user] = await seatbelt(fetchUser);



// If the function fails, then the first element
// will contain error and second will be null -> [Error:..., null]
const [err] = await seatbelt(badFunction);

if (err) {
  // handle error manually
}