🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Book a DemoInstallSign in
Socket

a-wait-forit

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

a-wait-forit

Async/await without try/catch

0.2.0
latest
Source
npm
Version published
Weekly downloads
63
-66.49%
Maintainers
1
Weekly downloads
 
Created
Source

Async/await without try/catch.

About

Allows to use async/await with no try/catch to handle errors.

Usage

Use await with function that might throw an error without try/catch

const { forit } = require('a-wait-forit')

async function(){
  const [ err, data ] = await forit(fetchSomething())
  // assert.equal(err, undefined)
  // assert.ok(data)

  const [ err ] = await forit(fetchThatThrowsError())
  // assert.ok(err)
}

Use async/await plus middlware with custom error handilng

wait bypasses any error occurred to a next middleware by default. To prevent that behaviour you might use custom onerror function. This helps to hide sensitive messages.

const { wait } = require('a-wait-forit')

app.use('*', wait(async (req, res) => {
  const result = await doSomething()

}, (err, next) => { next(new Error('Oops. Something went wrong.')) }))

Use wait + forit

const { wait, forit } = require('a-wait-forit')

app.use('*', wait(async (req, res, next) => {
  const [err, result] = await forit(fetchThatThrowsError())

  if(err) {
    res.status(500);
    res.send(new Error('Oops. Something went wrong.')) 
  }
}))

forit adn Promise's chains

forit returns the Promise but catches any error happend internally. If you want it play well in conjunction with .then, custom onerror handler should be specified.

const { forit } = require('a-wait-forit')

forit(fetchThatThrowsError())
  .then((_) => {
    // assert.ok(_[0] instanceof Error)
  })

forit(fetchThatThrowsError(), _ => { throw _ })
  .catch((_) => {
    // assert.ok(_ instanceof Error)
  })

Keywords

Promise

FAQs

Package last updated on 27 Dec 2022

Did you know?

Socket

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