Socket
Socket
Sign inDemoInstall

a-wait-forit

Package Overview
Dependencies
87
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    a-wait-forit

Async/await without try/catch


Version published
Maintainers
1
Install size
12.2 MB
Created

Readme

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

FAQs

Last updated on 27 Dec 2022

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