0.2.0 • Published 1 year ago

a-wait-forit v0.2.0

Weekly downloads
74
License
MIT
Repository
gitlab
Last release
1 year ago

Async/await without try/catch.

npm.io

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)
  })
0.2.0

1 year ago

0.1.6

5 years ago

0.1.5

5 years ago

0.1.4

5 years ago

0.1.3

5 years ago

0.1.2

5 years ago

0.1.1

5 years ago

0.1.0

5 years ago