await-protect
TypeScript icon, indicating that this package has built-in type declarations

1.0.17 • Public • Published

await-protect

Wraps promises to destructure values easily without having to nest with try catch. Gives easier error typing as well.

Usage

import protect from "await-protect"

async function do() {
    const json = { "msg": "Hello" }

    const [res, err] = await protect(
      axios.post(`${config.url}`, qs.stringify({
        data: new Buffer(JSON.stringify(a)).toString("base64")
      })))

    if (err) {
        console.log(err)
        return
    }

    console.log(res.data)
}

Instead of this:

async function do() {
    const json = { "msg": "Hello" }

    try {
        const res: AxiosResponse = await axios.post(`${config.url}`, qs.stringify({
            data: new Buffer(JSON.stringify(a)).toString("base64")
        }))

        console.log(res.data)
    } catch (err) {
        console.log(err)
    }
}

Readme

Keywords

Package Sidebar

Install

npm i await-protect

Weekly Downloads

2

Version

1.0.17

License

MIT

Unpacked Size

5.59 kB

Total Files

6

Last publish

Collaborators

  • nihiluis