A natural/modern/safer way to handle promise in async/await
$ yarn add promx
If you prefer using npm:
$ npm i -S promx
promx will allow you to write code like this:
import promx from 'promx'
async function main() {
const [err, res] = await promx(fetch('http://example.com/movies.json'))
if (err) console.warn('failed to load data')
else console.log({ res })
}
main()
Instead of this:
async function main() {
try {
await fetch('http://example.com/movies.json')
} catch (err) {
console.warn('failed to load data')
return
}
console.log({ res })
}
main()
timeout the promise if it took longer then 1 second:
import promx from 'promx'
async function main() {
const [err, res] = await promx(fetch('http://example.com/movies.json'), { timeout: 1000 })
if (err) console.warn('failed to load data')
else console.log({ res })
}
main()
promx(promise, [options])
Type: Promise
Receives a promise that will eventually resolve or reject
Type: Object
Type: Number
timeout duration in ms before resolving with timout error
MIT © mouafa