Skip to content
/ qfunc Public

Node.js queue of async/await functions that execute sequentially.

Notifications You must be signed in to change notification settings

anders94/qfunc

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

Node.js queue of async/await functions that execute sequentially.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published