Task
JavaScript utility that provides a consistent, clean return from promises. Particularly helpful when using await/async.
Install
NPM
npm install @simmo/task
Yarn
yarn add @simmo/task
Usage
task(<Promise>) => { error: <Any>, data: <Any> }
task
always returns an object. The object will contain two properties; error
and data
.
If the promise is resolved, error
will be null
and data
will contain anything the promise has returned. If the promise is rejected, error
will return the error provided by the promise and data
will be null
.
import task from '@simmo/task'
const { error, data } = await task(fetch('/some-api'))
if (error) {
// Failure
console.error(error)
} else {
// Success
console.log(data)
}
License
MIT © Mike Simmonds