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

@artisnull/asyncquence

v0.1.0

Published

Batch-run synchronous and asynchronous operations in sequence

Downloads

7

Readme

asyncquence

Run synchronous and asynchronous functions in sequence, with hooks, value pass through, and more!


Key Features

  • Executes and resolves functions in FIFO sequence, asynchronously
  • You can add to the sequence whenever you want
  • Add an array of functions, and it will be added in order into the sequence
  • Emits lifecycle events such as START, PROGRESS, or ERROR
  • Pause/Resume or Cancel whenever you want
  • Pass the results of the previous task through to the next one

Sample usage

Basic:
const asq = new Asyncquence()

const func1 = () => Promise.resolve(1)
const func2 = (x) => (x + 2)

const results = asq.add([
  [func1],
  [func2, [0]]
])

results[0].then(console.log) // 1
results[1].then(console.log) // 2

Add an array of tasks get an array of promises back in the order that we added the tasks. Pretty straightforward.

With passthrough:
const asq = new Asyncquence({
  passPrevValue: true
})

const func1 = () => Promise.resolve(4)
const func2 = (x) => (x + 10)

const results = asq.add([
  [func1],
  [func2]
])

results[0].then(console.log) // 4
results[1].then(console.log) // 14

Table of Contents

Reference

API


Reference

config

Defaults
const DEFAULT_CONFIG = {
  execImmediate: true,
  cancelOnError: false,
  passPrevValue: false,
  silent: false
}
Description
  • execImmediate
    • true: when first Task is added, it will be executed
    • false: the start() method is called to begin execution
  • cancelOnError
    • true: an error during execution will stop the rest of the sequence from completing
    • false: an error during execution is reported via rejection and the error lifecycle event, but the sequence continues to the next Task
  • passPrevValue
    • true: the result of the last Task is passed as the last argument to the next Task
    • false: each Task is executed independently
  • silent
    • true: silences default event messages
    • false: default events are logged to console

Events

Not all events pass an argument to the event listener
Format: EVENT_NAME: (argument)


STATUS_CHANGE : ('READY'|'PAUSED'|'RUNNING'|'STOPPED')
START
PROGRESS :
{
  remaining: numRemaining // int
  completed: numCompleted // int
  percentComplete: percentString //String
}
COMPLETE
PAUSE : (nextTask)
RESUME : (nextTask)
CANCEL
ERROR: (err)

Task

The format of function and arguments that asyncquence expects.
Can be one of the following:

const Task = [fn, [args]]
// OR
const Task = {
  method: fn,
  args: [args]
}

Adding single tasks is simpler: asq.add(fn, [args])



API

new Asyncquence(config)

Returns new Asyncquence instance with the specified config

const asq = new Asyncquence()

add(Task) : Promise[]

add([Task,[...Tasks]]) : Promise[]

Adds a task(s) to the back of the queue to be executed. Returns an array of promises at indices corresponding to the index of each task added. See Task for reference.

Triggers 'READY' status change when adding a task to an empty queue if config option execImmediate:false
Triggers START Event when adding a task to an empty queue if config option execImmediate:true

// Single task
const res = asq.add(Task) // res[0] has Promise for this task

// Multiple tasks
const res = asq.add([Task1, Task2]) // res[0] for Task1, res[1] for Task2

addEventListener(name:Event, callback:function)

Registers a function to call when the specified event takes place. See Event for reference.

asq.addEventListener('STATUS_CHANGE', cb)

cancel()

Stops execution of current Task and empties queue. Doesn't affect event listeners

Triggers CANCEL Event
Triggers 'STOPPED' status change

asq.cancel()

clear()

Immediately empties queue and removes all event listeners, dangerous
Use cancel() to safely stop an asyncquence

Won't trigger any event listeners, as they are removed

asq.clear()

clearEventListeners()

Immediately removes all event listeners

asq.clearEventListeners()

pause()

Pauses execution of sequence. Any currently running Tasks will complete, but no future Tasks will be executed until resume() is called

Triggers 'PAUSED' status change

asq.pause()

removeEventListener(name:Event, callback:function)

Removes the specified event listener

asq.removeEventListener('STATUS_CHANGE', cb)

resume()

Resumes sequence execution from a paused state.

Triggers 'RESUME' status change

asq.resume()

start()

Begins execution of the first element in the sequence. Not applicable if config option: execImmediate:true, as execution begins automatically

Triggers START Event
Triggers 'RUNNING' status change

asq.start()

LICENSE

MIT License

Copyright (c) 2018 artisnull

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.