Giter VIP home page Giter VIP logo

qfunc's Introduction

qfunc

Queue a number of async functions which get executed sequentially.

Example:

const { Queue } = require('qfunc');

const queue = new Queue();

queue.enqueue(async () => {
    await new Promise(resolve => setTimeout(resolve, 1000));
    console.log('A');
});

queue.enqueue(async () => {
    await new Promise(resolve => setTimeout(resolve, 500));
    console.log('B');
});

Output:

A
B

Enqueue any number of functions in a FIFO queue and they will run sequentially, awaiting the completion of each before starting the next.

This is useful when you want to asyncronously add functions to a queue without woring that they will execute in the order in which they were added.

You can see the depth of the queue by reading queue.length() like this:

const { Queue } = require('qfunc');

const queue = new Queue();

queue.enqueue(async () => {
    await new Promise(resolve => setTimeout(resolve, 500));
    console.log('A');
});

queue.enqueue(async () => {
    await new Promise(resolve => setTimeout(resolve, 500));
    console.log('B');
});

console.log(queue.length());

In the above example, the queue length will be 1 because the first function will already have been dequeued and started leaving only one function on the queue.

You can also read isEmpty() which returns a boolean:

const { Queue } = require('qfunc');

const queue = new Queue();

queue.enqueue(async () => {
    await new Promise(resolve => setTimeout(resolve, 500));
    console.log('A');
});

queue.enqueue(async () => {
    await new Promise(resolve => setTimeout(resolve, 500));
    console.log('B');
});

console.log(queue.isEmpty());

The above example results in false because the queue is not empty.

The implementation is super simple and far fewer lines than even this README.md. See what I mean by looking here: https://github.com/anders94/qfunc/blob/main/lib/index.js

Here's a more expressive example that uses an interval to continually show the length of the queue and prove that things operate in order:

const { Queue } = require('qfunc');

const timeout = (ms) => {
    return new Promise(resolve => setTimeout(resolve, ms));
};

const queue = new Queue();

const i = setInterval(() => console.log(queue.length()), 250);

console.log('queue 1');
queue.enqueue(async () => {
    console.log('start 1');
    await timeout(1000);
    console.log('end 1');
});

console.log('queue 2');
queue.enqueue(async () => {
    console.log('start 2');
    await timeout(1000);
    console.log('end 2');
});

setTimeout(() => {
    console.log('queue 3');
    queue.enqueue(async () => {
	console.log('start 3');
	await timeout(1000);
	console.log('end 3');
	clearInterval(i);
    }, 1100);
});

Output:

queue 1
start 1
queue 2
queue 3
2
2
2
end 1
start 2
1
1
1
1
end 2
start 3
0
0
0
0
end 3

License

MIT

qfunc's People

Contributors

anders94 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.