p-resolvify
v2.1.0
Published
Handle promise rejection like resolved
Downloads
17
Readme
p-resolvify
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)