Skip to content
/ promx Public

⛑ a natural/modern/safer way to handle promise in async/await

Notifications You must be signed in to change notification settings

mouafa/promx

Repository files navigation

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

About

⛑ a natural/modern/safer way to handle promise in async/await

Resources

Stars

Watchers

Forks

Packages

No packages published