await-protect
v1.0.17
Published
Concise error handling when using promise await
Downloads
19
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)
}
}