Socket
Socket
Sign inDemoInstall

await-on

Package Overview
Dependencies
0
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    await-on

really simple error handling with await/async


Version published
Weekly downloads
259
increased by21.6%
Maintainers
1
Install size
5.74 kB
Created
Weekly downloads
 

Readme

Source

😪 await-on

npm node npm npm Travis Coveralls github

really simple error handling with await/async

inspired by await-to-js whose creator Dima Grossman originally blogged about using destructuring array assignment

Overview

This package basically provides 2 main syntaxes to choose from:

const on = require('await-on');

const fetchData = () => new Promise(/*...*/);

const [err, data] = await on(fetchData());
const [err, data] = await fetchData().handle(); //prototype extension

The goal is to avoid the built-in approach using the try/catch block pattern:

try{
	const data = await fetchData();
	res.send(data);
}catch(err) {
	res.send(err);
}

Quick Usage

using on with the function fetchData which returns a Promise that resolve to some result data:

const {on} = require('await-on');
const fetchData = () => new Promise(/*...*/);

async function foo(req,res) {
	const [err, data] = await on(fetchData());
	if(err) res.send(err);
	else res.send(data);
}

using the prototype extension handle on Promise types is a bit cleaner, and its potentially more readable because its also using the same chaining pattern already standard for working with Promises 🌟 :

require('await-on');

async function foo(req,res) {
	const [err, data] = await fetchData().handle();
	!err ? res.send(data) : res.send(err);
}

Type fuzziness

Non-promises will passthrough same as the behavior of the native await

const [err,answer] = await on(42); //not a promise but ok no big deal
console.log(answer) //> 42

Decorator approach

A decorator on.handler is also provided to wrap promise bearing functions with the handling functionalities:

const {handler} = require('await-on');
let fetchData = () => new Promise(/*...*/);
fetchDataAndHandle = handler(fetchData);

async function foo(req,res) {
	const [err, data] = await fetchDataAndHandle();
	!err ? res.send(data) : res.send(err);
}

Promises/A+ compliant support

You never know what kind of promise you'll get from an external dependency. It can be from Bluebird, Babel polyfill or some other library. on should work with any promise object can then, such as the one provided by the popular bluebird package:

const {on} = require('await-on');
const Bluebird = require('bluebird')

const fetchData = () => new Bluebird(/*...*/);
const [err, data] = await on(fetchData());

License

MIT License. See License in the repository.

Keywords

FAQs

Last updated on 05 Aug 2019

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc