0.1.2 • Published 2 years ago
@ruwan.m.s/promise-utils v0.1.2
@ruwan.m.s/promise-utils
Introduction
Welcome to the documentation for the @ruwan.m.s/promise-utils npm package! This package provides utility functions for working with Promises.
Installation
To install @ruwan.m.s/promise-utils, you can use npm. Open your terminal and run the following command:
npm install @ruwan.m.s/promise-utilsUsage
Once you have installed the package, you can import it into your project using the following syntax:
TypeScript (TS)
import promiseUtils from '@ruwan.m.s/promise-utils';JavaScript (JS)
const promiseUtils = require('@ruwan.m.s/promise-utils');API Reference
inlinePromise: Function for executing a Promise inline. Return the as[result, error]inlinePromiseObject: Function for executing a Promise inline. Return the as{data, error}inlinePromiseAll: Function for executing an array of Promises inline usingPromise.all(). Returns list of[result, error]inlinePromiseObjectAll: Function for executing an array of Promises inline usingPromise.all(). Returns list of{data, error}
Example Usage
Let's consider simple delay function
const delay = (ms: number): Promise<number> => {
return new Promise(resolve => setTimeout(() => resolve(ms), ms));
}inlinePromise()
Here's an example of how you can use the inlinePromise function:
const [delayTime, error] = await inlinePromise(delay(300));
if (error) {
// Error Handling
}
console.log(delayTime); // Output: 300inlinePromiseAll()
Here's an example of how you can use the inlinePromiseAll function:
const res= await inlinePromiseAll([
delay(200),
delay(400),
]);
const [result1, error1] = res.next<number>();
console.log(result1) // Output: 200
const [result2, error2] = res.next<number>();
console.log(result2) // Output: 400inlinePromiseObject()
Here's an example of how you can use the inlinePromise function:
const {data: delayTime, error} = await inlinePromiseObject(delay(300))
if(error){
// Error Handling
}
console.log(delayTime) // Output: 300inlinePromiseObject()
Here's an example of how you can use the inlinePromiseObjectAll function:
const res= await inlinePromiseObjectAll([
delay(200),
delay(400),
]);
const {data:result1} = res.next<number>();
console.log(result1) // Output: 200
const {data:result2} = res.next<number>();
console.log(result2) // Output: 400