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

@nasimhuq/queue-of-promises

v0.1.4

Published

Queue of promises

Downloads

2

Readme

queue-of-promises

Queue of promises. All promises are executed concurrently as they are added to the input queue. Regardless of which promise resolved earlier, output queue will always keep the order it was executed

To abort queue of promises please checkout abort-promise-queue

Zero-dependency, total size: 1755 B uncompressed and 1002 B gzip-compressed

Output queue returns an object { config, data, error }

  • On successful response, data will contain the response body.
  • On error response, error will contain the error message.

Install

npm install --save @nasimhuq/queue-of-promises

Typical usage

Require as CJS

const queueOfPromises = require('@nasimhuq/queue-of-promises');

Import as ES6 Module

import queueOfPromises from '@nasimhuq/queue-of-promises';

Examples

Example 1: Single batch of requests

import queueOfPromises from '@nasimhuq/queue-of-promises'

const api = (config) => {
    return new Promise((resolve, reject) => {
        const { output } = config
        setTimeout(() => {
            if (output.reject) {
                reject(output)
            } else {
                resolve(output)
            }
        }, output.duration)
    })
}

const multi = 1000

const reqConfigList = [
    {
        output: {
            key: 'First',
            duration: 1000 + multi,
        },
    },
    {
        output: {
            key: 'Second',
            duration: 500 + multi,
        },
    },
    {
        output: {
            key: 'Third',
            duration: 1300,
            reject: true,
        },
    },
    {
        output: {
            key: 'Fourth',
            duration: 1300 + multi * 2,
            reject: true,
        },
    },
    {
        output: {
            key: 'Fifth',
            duration: 1000 + multi * 3,
            reject: true,
        },
    },
    {
        output: {
            key: 'Sixth',
            duration: 900 + multi,
        },
    },
    {
        output: {
            key: 'Seventh',
            duration: 700 + multi,
        },
    },
]

const test_single_batch = async (resolve) => {
    const { inputQueue, outputQueue } = queueOfPromises(api)
    reqConfigList.forEach((config) => {
        inputQueue.next(config)
    })
    const start = Date.now()
    try {
        for await (const res of outputQueue) {
            console.log(res)
            const end = Date.now()
            console.log('duration: ', end - start)
        }
    } catch (e) {
        console.log('something went wrong!')
    }
    console.log('finished')
    resolve() // this resolve is used for display purpose only
}

const test_multiple_batches = async (resolve) => {
    const { inputQueue, outputQueue, closeQueue } = queueOfPromises(api, true, 500)
    inputQueue.next(reqConfigList[0])
    inputQueue.next(reqConfigList[1])
    inputQueue.next(reqConfigList[2])

    const start = Date.now()
    setTimeout(() => {
        console.log('new Batch:', Date.now() - start)
        inputQueue.next(reqConfigList[3])
        inputQueue.next(reqConfigList[4])
        inputQueue.next(reqConfigList[5])
        inputQueue.next(reqConfigList[6])
    }, 3000)

    setTimeout(() => {
        closeQueue() // no more adding new fetch config to inputQueue after this call.
    }, 5000)

    try {
        for await (const res of outputQueue) {
            console.log(res)
            const end = Date.now()
            console.log('duration: ', end - start)
        }
    } catch (e) {
        console.log('something went wrong!')
    }
    console.log('finished')
    resolve() // this resolve is used for display purpose only
}

const test_multiple_batches_promise = async () => {
    return new Promise(async (resolve) => {
        test_multiple_batches(resolve);
    })
}

const test_single_batch_promise = async () => {
    return new Promise(async (resolve) => {
        test_single_batch(resolve)
    })
}

const allTests = async () => {
    console.log('---------------------- single batch -------------------------')
    await test_single_batch_promise()
    console.log('------------------------multiple batches ------------------')
    await test_multiple_batches_promise()
}

allTests()

queue-of-promises can be used in node.js