Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

promise-chain-executor

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

promise-chain-executor

Executes promises in a chain, allowing results from previous promise to affect following ones

  • 3.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
4
decreased by-50%
Maintainers
1
Weekly downloads
 
Created
Source

Promise Chain Executor

A tool for executing promises in a chain, where the output of previous promises are used to change the behaviour of following promises.

A PromiseSupplier is passed to the Promise Chain Executor, this supplier is responsible for creating a new Promise based on the output from the previous Promise.

This can be demonstrated in a simple form with the following example:

const promiseSupplier = async (previousResult:number|null)=> {
  const newResult = (previousResult ?? 0) + 1
  if (newResult > 9) {
    return null
  }
  return newResult
}

const results = await index.execute<number>(promiseSupplier)
// results: [1,2,3,4,5,6,7,8,9]

This is not a very useful example of the executor, a typical use case for the Promise Chain Executor is when you are calling an API to iterate through some results and you receive an offset ID to continue from.

const promiseSupplier = async (previousOffset:string|null)=> {
  
  const newResults = await callApiForNextResults(previousOffset)
  if (newResults.length === 0) {
    return null; // if we are at the end of the results, return null to terminate the chain
  }
  
  await doSomethingWithResults(newResults)
  
  const lastResult = newResults[newResults.length - 1]
  const newOffset = lastResult.id // update the offset to start from the most recent result
  return newOffset
}

// process results from API by chaining promises together for each new offset
await index.execute<string>(promiseSupplier)

Installation

npm install --save promise-chain-executor

Keywords

FAQs

Package last updated on 23 Mar 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc