promx
v2.0.2
Published
a natural/modern/safer way to handle promise in async/await
Downloads
166
Maintainers
Readme
Installation
$ yarn add promx
If you prefer using npm:
$ npm i -S promx
Usage
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()
API
promx(promise, [options])
promise
Type: Promise
Receives a promise that will eventually resolve or reject
options
Type: Object
options.timeout
Type: Number
timeout duration in ms before resolving with timout error
License
MIT © mouafa