p-resolvify

Handle promise rejection like resolved

Usage no npm install needed!

<script type="module">
  import pResolvify from 'https://cdn.skypack.dev/p-resolvify';
</script>

README

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)