Skip to content

Concise rust and go-like error handling when using promise await for Typescript.

Notifications You must be signed in to change notification settings

nihiluis/await-protect

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

30 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

Concise rust and go-like error handling when using promise await for Typescript.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published