npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ts-async-loop

v1.3.0

Published

asycnhrnous foo/map loop

Downloads

36

Readme

ts-async-loop

A mean to execute for/map loop asynchronously

One can execute async actions one by one, or two by, ar three by three, etc...

Let's say you want to execute a function named asyncFunc that takes a string and a number as parameters and return a string.

To call asyncFunc once at a time:

    const loop = makeAsyncLoop(asyncFunc)
    
    const results = await loop(
      ['A', 1],
      ['B', 2],
      ['C', 3],
    )

or, if your parameters are already in an array:

    const loop = makeAsyncLoop(asyncFunc)
    
    const results = await loop(...parametersArray)

First asyncFunc is called given 'A' and 1 as parameters. After the first execution, it is called using 'b' and 2 parameters, and so on. results ends up to be an array: first item is the result of the first asyncFunc execution, second items it the result of the second call, and so on.

To call asyncFunc two at a time:

   const loop = makeAsyncLoop(asyncFunc, {maxExecution: 2 })
   
   const results = await loop(
     ['A', 1],
     ['B', 2],
     ['C', 3]
   )

asyncFunc('A', 1) and asyncFunc('B', 2) are executed first. One one of them is terminated, asyncFunc('C', 3) is called.

Whatever end of executin is, first item od resutls array is always the result of the first asyncFunc execution, second items is always it the result of the second call, and so on.

You can set any number you want to maxExecution but don't forget that this lib runs in the vent-loop: the higer maxExecution the more event-loop has work to do.

By default ts-async-lopp checks every 100ms if it can trigger a new execution. To reduce the gap between execution yo can set your own value using waitingDuration:

    const loop = makeAsyncLoop(
      asyncFunc, 
      { waitingDuration: 10 } // checks every 10ms
    ) 
    
    const loop = makeAsyncLoop(
      asyncFunc, 
      {
        maxExecution: 2, 
        waitingDuration: 200 // checks every 2000ms
      }
    ) 
    

Follow progression

Two callbacks are avaialable: onStart and onStop. When provided, onStart is called each time an execution is launched.

Parameters:

  index: execution parameters index
  params: parameters of the execution
  currentExecutionCount: execution curently running (including the one for which onStart is calling)

When provided, onStop is called each time an execution is terminated.

Parameters:

  index: execution parameters index
  params: parameters of the execution
  currentExecutionCount: execution curently running (the one that just stop in not included)
  totalExecutionCount: total numbers of execution

They are parts of the makeAsyncLoop's options:

const onStart = ({ index, params, currentExecutionCount }) => console.log(`Start execution ${index}, with params ${params} (current number of tasks: ${currentExecutionCount}).`)

const onStop = ({ index,
 currentExecutionCount }) => console.log(`Execution ${index} is done(current number of tasks: ${currentExecutionCount}).`)

const asyncLoop = makeAsyncLoop(
  asyncFunc,
  {
    maxExecution: 2,
    onStart,
    onStop
  }
)

Error

If any execution failed, an error is thrown and not other execution is performed.

MakeAsyncLopp throw a specific error: isAsynLoopError, which have the following properties:

  error the error thrown by an execution
  index index of the erroneous execution
  params parameters of the erroneous execution
  currentExecutionCount number of exectuion when the error has been thrown
  totalExecutionCount: total number of executions

For typescript user, a isAsynLoopError function allow to cast an error to an isAsynLoopError:

} catch (error) {
  if (isAsynLoopError(error)) {
    console.error(`Error from Async loop: \n\tmessage:${error.error} \n\tname:${error.name} \n\tcurrentExecutionCount${error.currentExecutionCount} \n\tindex${error.index} \n\tparams${error.params}`)
    console.error(error.error.stack)
  } else {
    throw error
  }
}

This package include esm, commonjs & umd distribution, along with a d.td declarations file


Options:

interface MakeAsyncLoopOptions {
  maxExecution?: number; Maximum number of simultanous executions
  waitingDuration?: number; Check every <waitingDuration> ms is a new exectuoin can be launched
  onStart?: a callback triggrered when an execution starts, 
  onStop?: a callback triggrered when an execution stops, 
}

Known issues

  • Parameters are not typed