Welcome to the documentation for the @ruwan.m.s/promise-utils
npm package! This package provides utility functions for working with Promises.
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-utils
Once you have installed the package, you can import it into your project using the following syntax:
import promiseUtils from '@ruwan.m.s/promise-utils';
const promiseUtils = require('@ruwan.m.s/promise-utils');
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}
Let's consider simple delay function
const delay = (ms: number): Promise<number> => {
return new Promise(resolve => setTimeout(() => resolve(ms), ms));
}
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: 300
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: 400
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: 300
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