go-promise
v0.0.3
Published
Handle promise inline (inspired by Golang's error handling style)
Downloads
7
Readme
go-promise — Handle promises inline for JS
go-promise
is a tiny util library, help you handle promises inline (inspired by Golang's error handling style). It helps you read code better, and is more convenient to handle error where there are multiple Promise
s or with a huge logic code.
It can be used in browser and NodeJS enviroment
1. How to use
The library exposes only 1 function goPromise
and return an array with 2 values.
When the promise is rejected:
- The 1st value will be
Error
- The 2nd value will be
undefined
When the promise is resolved
- The 1st value is
null
- The 2nd value is the resolve data
goPromise(Promise<T>): Promise<[Error | null, T | undefined]>
2. Example
// es6 import
import goPromise from "go-promise";
// or standard import
const goPromise = require("go-promise");
async function yourFunc() {
const [err1,data1] = await goPromise(YourPromise1Func());
if (err !== null) {
// handle error1;
}
// handle data1
const [err2,data2] = await goPromise(YourPromise1Func());
if (err2 !== null) {
// handle error1;
}
// handle data1
}
3. Install
Install using Yarn
$ yarn add go-promise
Install using NPM
$ npm i go-promise
4. License and Contribution
go-promise
is availabe under MIT license.
Feel free to open a thread for question or suggestion