Skip to content

Latest commit

 

History

History
97 lines (72 loc) · 3.04 KB

README.md

File metadata and controls

97 lines (72 loc) · 3.04 KB

ℹ️ Before submitting an issue to this repo - Ensure it's a issue with the code in this repo, you may meet some issue from rollup config - please take a look in this document Rollup and vite Vite

Why

As a front-end engineer dealing with asynchronous code, you might find that the default browser queue loop stack doesn't always behave as expected in special contexts.
This library provides different modes for queue management: single parallel

Original event loop

javascript event loop

  • Functions can be:
  • Synchronous - execute step-by-step ()
  • Asynchronous - executed according to the rules of the Event loop. Learn more about the Event loop Link (Implement of Stack DS)

With AsyncQueue mode single

  • Asynchronous functions are executed with a new approach
    • Functions are pushed into an array[]
    • One function at a time is run from the array (following the Data Structure approach), called by the normal web browser call stack.
    • After the function finishes (either rejected or fulfilled), it is removed from the array, and the next function is executed.
    • Only one function is executed at a time.

With AsyncQueue mode parallel

  • Extends single mode by adding:
    • A parallelLimit parameter.
    • Executes both synchronous and asynchronous functions concurrently.

Running it Locally

  • Clone the project:
$ git clone https://github.com/b0yblake/AsyncQueue
  • Install:
$ npm i asyncqueue-custom

Usage

import { createAsyncQueue } from "asyncqueue-custom"
const queueProcess = createAsyncQueue()

// Some async functions
function async doSomeThing1() {
  setTimeout(() => {
    console.log('1')
  }, 10000)
} //Finish in 10s
function async doSomeThing2() {
  setTimeout(() => {
    console.log('2')
  }, 2000)
} //Finish in 2s
function async doSomeThing3() {
  setTimeout(() => {
    console.log('3')
  }, 5000)
} //Finish in 5s

// Push to list
const opts = {
  parallelConcurrent: false,    // True for parallel mode
  approach: 'fifo',             // Optional, default fifo
  autostart: false              // Auto start queue process after push
}
await queueProcess.push(doSomeThing)
await queueProcess.push(doSomeThing2)
await queueProcess.push(doSomeThing3)

await queueProcess.start(opts)
// Expected output order: 1, 2, 3
// Functions run and print in the order they finish execution

Contributing

PRs are welcomed. If you think there are any missing useful layouts or patterns, please create an issue or submit a PR.

If there is any issue, it will be logged in the tslint.log file.

About

This project is developed by b0yblake. I love building products and sharing knowledge.

Be my friend on