promx
TypeScript icon, indicating that this package has built-in type declarations

2.0.2 • Public • Published

A natural/modern/safer way to handle promise in async/await

npm version codecov Conventional Commits Build Status

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

Package Sidebar

Install

npm i promx

Weekly Downloads

59

Version

2.0.2

License

MIT

Unpacked Size

5.55 kB

Total Files

6

Last publish

Collaborators

  • mouafa