1.0.0 • Published 1 year ago

@f1stnpm3/nam-hic-consequatur v1.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

@f1stnpm3/nam-hic-consequatur

Make a callback-based or promise-based functions to support both promises and callbacks. Uses the native promise implementation. with typescript support.

npm Package License

Installation

npm i @f1stnpm3/nam-hic-consequatur

Import or Require

import ppc from "@f1stnpm3/nam-hic-consequatur";
OR
import {fromCallback,fromPromise} from "@f1stnpm3/nam-hic-consequatur";
OR
const ppc = require("@f1stnpm3/nam-hic-consequatur");
OR
const fromCallback = require("@f1stnpm3/nam-hic-consequatur");
OR
const fromPromise = require("@f1stnpm3/nam-hic-consequatur");

Here is an example usage of fromCallback function:

Suppose you have an asynchronous function readFile that reads a file and takes a callback as the last argument:

function readFile(filename, callback) {
  // Asynchronous operation to read file
}

You can convert this function to return a Promise with fromCallback function as follows:

import {fromCallback} from "@f1stnpm3/nam-hic-consequatur";

const readFilePromise = fromCallback(readFile);

// You can now call the function with a Promise:
readFilePromise(filename)
  .then((data) => console.log(data))
  .catch((err) => console.error(err));

// Or with a callback:
readFilePromise(filename, (err, data) => {
  if (err) {
    console.error(err);
  } else {
    console.log(data);
  }
});

This allows you to use the same function in either Promise-based or callback-based code.

Here is an example usage of fromPromise function:

Suppose we have a function getUser that returns a promise:

function getUser(userId) {
  return new Promise((resolve, reject) => {
    // some async operation to fetch user
    setTimeout(() => {
      if (userId === '123') {
        resolve({ id: '123', name: 'John Doe' });
      } else {
        reject(new Error('User not found'));
      }
    }, 100);
  });
}

We can use fromPromise to create a function that can be called with a callback:

import {fromPromise} from "@f1stnpm3/nam-hic-consequatur";

const getUserCallback = fromPromise(getUser);

getUserCallback('123', (err, user) => {
  if (err) {
    console.error(err);
  } else {
    console.log(user); // { id: '123', name: 'John Doe' }
  }
});

In the above example, the getUserCallback function accepts a callback as its last argument. If a callback is provided, it calls the original getUser function with the provided arguments and passes the result or error to the callback. If no callback is provided, it returns a promise.

We can also call getUserCallback without a callback to get a promise:

getUserCallback('123')
  .then(user => console.log(user)) // { id: '123', name: 'John Doe' }
  .catch(err => console.error(err));

License

@f1stnpm3/nam-hic-consequatur is licensed under the MIT License.

ES2023typescriptIteratorsyntaxtouchUint32ArrayperformancedebuglimitwordbreakpluginReflect.getPrototypeOfxdgshamfluxRxJSconcatratelimitYAMLairbnbimmerECMAScript 2021call-boundtypeschannelcode pointskinesisthroatES2017TypeScriptrequirelimitedjapanesevalidatevariablesdebuggernopeamazonarraybufferpredictablewhatwgUint16ArrayfoldergetoptstylesresolveArray.prototype.findLasttests3defaultbuffersUnderscore$.extendfull-widthArray.prototype.flat256packagesreact-testing-libraryeventEmitterURLtrimECMAScript 2020upispreserve-symlinksfastcloneformsduplexemitpropertiesargumentsbddstartreplayes2017fullregexpjshintcall-bindRegExp.prototype.flagslaunchjestglacierirqstoragegatewayrm -rfchinesefilterauthoptionexecinternalObject.definePropertysnsconsolebcryptzerospawnstylingpostcsscharacterdatastructurelesscsspostcss-plugincreatebyteshrinkwraploadingvisualpipevestcensorautoscalingwriteexpressiones7propweakseteditoroutputES5agentconcatMapESnextECMAScript 2015parsestyled-componentsfastifyoperating-systemfilecirculartyped arrayinternal slotmkdirsrandomvalueJSON-Schemafast-clonewrapfastsuperstructreducerES2015Array.prototype.filterfpsmkdirpArrayBufferwritableframeworktypeofreactauthenticationec2fullwidthtelephoneoncehasnested cssfindupgetprototermchildECMAScript 6artpushdeep-copyiterationlessaccessibilitypackagehooksmapjavascriptqueuemetadatasqsdynamodbInt32ArraysafedefinePropertyiamutilitiesassertsimmutablermdirietimebindrm -frconfigurableextendhandlerses2016zod0RFC-6455polyfillfile systemflatargvObject.getPrototypeOfReactiveExtensionsio-tsnegative zeroPromiseloadbalancingeast-asian-widthmime-dbutil.inspectbuffersomequerystringtsfunctionalenumerablejasmineminimaldeepmoduleArray.prototype.flattenrfc4122chaireact-hook-formmochaelasticachedescriptorsendpointeslint-plugingroupoffsetserializediffglobaltrimRightdefinegenericslooktestercommandeverymobileerrormimetypesargsterminaldirObject.keystypelistenerspatches5tc39webMappasswordi18nworkflowhardlinksUint8ArrayECMAScript 5xhrstateeslintconfigyamldirectorymakextermInt16Arraypyyamlimportexporteslintplugincachedeep-cloneHyBiclihttpbrowserslistWebSocketsyupebsprotocol-buffersfast-deep-copyArrayfseventsbootstrap cssjoimodulesES7open_.extendgetterpreprocessorECMAScript 2016StyleSheetObject.isstructuredClonees6arrayphonepackage managerfigletcorswebsitecoerciblelinkmapreducecallboundredux-toolkitshebangcloudsearchjsonpathBigInt64Arrayspinnerfixed-widthbatchansilengthfrommonorepopnpm9macosschemamatchAllsuperagentindicatorgradients cssmimefetchreadablereduxhas-owncommand-linemrukeycmdwarninges2018dependency managerwaittddECMAScript 2019Array.prototype.includesdeepcopymiddlewareprunees-shimsObject.entriesArray.prototype.flatMaptoobjectgradients css3typesafefastcopygroupByeventDispatcherpromisewalkingeventscompilerArray.prototype.containscsslazysymlinktoSortedequaltypedarraysequalityroute53react-hooksnamesECMAScript 2018stringifyforEachgetOwnPropertyDescriptorflatMapsorttrimLeftinstallObject.fromEntriesES2022prettyArrayBuffer#slicedom-testing-librarycloudwatch
1.0.0

1 year ago