p-resolvify

2.1.0 • Public • Published

p-resolvify

styled with prettier gzip size downloads module formats: umd, cjs, and es

Build Status Code Coverage MIT License PRs Welcome

Handle promise rejection like resolved

Install

yarn add p-resolvify

Usage

import resolvify from 'p-resolvify'
<script src="https://unpkg.com/p-resolvify"></script>

API

resolvify function

resolvify(function, options?)

return a new function returns a promise always resolve.

resolvify promise

resolvify(promise, options?)

return a promise always resolve.

options.handler

  • type: function

Promise reject error will pass through handler and then return

options.to

return value as array

const [error, result] = await resolvify(promise, {to: true})
console.log(error, result)

resolvify.to

shortcut for options.to

resolvify.to(promise)

// equals to

resolvify(promise, {to: true})

Examples

const maybeReject = () =>
  Math.random() > 0.5
    ? Promise.resolve(true)
    : Promise.reject(new Error('error.'))

Before, without resolvify

let result = false

try {
  result = await maybeReject()
} catch {}

console.log(result)

After, with resolvify

// `try/catch` is not needed

const result = await resolvify(maybeReject)
console.log(result)

Package Sidebar

Install

npm i p-resolvify

Weekly Downloads

2

Version

2.1.0

License

MIT

Unpacked Size

14.8 kB

Total Files

9

Last publish

Collaborators

  • fisker