await-protect

Concise error handling when using promise await

Usage no npm install needed!

<script type="module">
  import awaitProtect from 'https://cdn.skypack.dev/await-protect';
</script>

README

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