0.1.2 • Published 11 months ago

@ruwan.m.s/promise-utils v0.1.2

Weekly downloads
-
License
MIT
Repository
github
Last release
11 months ago

@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-utils

Usage

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 using Promise.all(). Returns list of [result, error]
  • inlinePromiseObjectAll: Function for executing an array of Promises inline using Promise.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: 300

inlinePromiseAll()

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

inlinePromiseObject()

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

inlinePromiseObject()

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
ansi-escapesansi-regexansi-stylesanymatchbabel-plugin-istanbulbabel-preset-current-node-syntaxargparsebalanced-matchbabel-jestbabel-preset-jestbabel-plugin-jest-hoistbserbrowserslistcallsitescamelcasebraceschalkchar-regexci-infobrace-expansioncocolor-convertcollect-v8-coveragecjs-module-lexerbuffer-fromconvert-source-mapcliuicross-spawndedentcaniuse-litedeepmergedetect-newlinediff-sequencesdebugemitteryconcat-mapcolor-nameescaladeerror-exelectron-to-chromiumexecaexpectexitfast-json-stable-stringifyfill-rangefb-watchmanemoji-regexesprimaescape-string-regexpfunction-bindfind-upfs.realpathget-streamglobgraceful-fsgensyncget-caller-fileget-package-typehasglobalsimport-localhas-flaghuman-signalshtml-escaperinflightinheritsimurmurhashis-arrayishis-streamis-core-moduleistanbul-lib-coverageistanbul-lib-instrumentis-generator-fnistanbul-lib-source-mapsistanbul-reportsjest-changed-filesjest-circusistanbul-lib-reportis-fullwidth-code-pointjest-clijest-diffjest-configjest-docblockjest-eachjest-environment-nodejest-haste-mapjest-get-typejest-leak-detectorjest-message-utiljest-mockjest-matcher-utilsjest-pnp-resolverjest-regex-utiljest-resolvejest-resolve-dependenciesjest-runnerjest-runtimejest-snapshotjest-utiljest-watcherjest-validatejest-workeris-numberjs-yamljs-tokensisexejsesclevenjson-parse-even-better-errorsjson5kleurlocate-pathmicromatchlines-and-columnsmerge-streammimic-fnlru-cachemake-dirnatural-comparemakeerrornormalize-pathminimatchmsnpm-run-pathonetimep-limitnode-int64parse-jsonnode-releasesoncepath-keypath-existsp-tryp-locatepicomatchpath-is-absolutepiratespath-parsepretty-formatpure-randpromptsresolvereact-ispkg-dirpicocolorsresolve.exportssemverresolve-cwdresolve-fromsignal-exitrequire-directoryslashshebang-commandshebang-regexsource-map-supportstack-utilsstring-lengthsisteransistrip-ansistrip-final-newlinestrip-bomstrip-json-commentssupports-colorsource-mapstring-widthtest-excludeto-fast-propertiessprintf-jssupports-preserve-symlinks-flagtype-festtmplv8-to-istanbulwalkerupdate-browserslist-dbto-regex-rangewhichwrap-ansiwrite-file-atomictype-detectyocto-queueyargsyargs-parserwrappyy18nyallist
0.1.2

11 months ago

0.1.1

11 months ago

0.1.0

11 months ago