Skip to content

Instantly share code, notes, and snippets.

@christroutner
Last active December 5, 2023 09:15
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save christroutner/cb904e2b0b85db53c8544fd8b60be6d1 to your computer and use it in GitHub Desktop.
Save christroutner/cb904e2b0b85db53c8544fd8b60be6d1 to your computer and use it in GitHub Desktop.
Promise queue and auto-retry example
/*
Tests the promise queue (p-queue) and promise retry (p-retry) libraries.
*/
"use strict";
const { default: PQueue } = require("p-queue");
const queue = new PQueue({ concurrency: 1 });
const pRetry = new require("p-retry");
// This is the parent the function that is called to start the program.
async function runTest() {
try {
// Add a series of long-running functions to the queue.
// Using the () => converts it into an anonymous function and allows me
// to pass arguments through the queue.
queue.add(() => loadFunc(1));
queue.add(() => loadFunc(2));
queue.add(() => loadFunc(3));
console.log(`queue.size: ${queue.size}`);
} catch (err) {
console.log(`Error in runTest(): `, err);
}
}
runTest();
// A promise-based sleep function.
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
// This function 'loads' the long-running function by wrapping it in a p-retry
// function.
async function loadFunc(num) {
// Wrap longFunc in a retry function. The '() =>' notation wraps longFunc()
// inside an anonymous function, which lets me pass arguments to the function.
await pRetry(() => longFunc(num), {
onFailedAttempt: async error => {
console.log(
`Attempt ${error.attemptNumber} failed. There are ${
error.retriesLeft
} retries left.`
);
console.log(`Waiting 2 seconds before trying again.`)
await sleep(2000)
},
retries: 5
});
}
// This is a mock of a long-running promise-based function. It lasts 5 seconds,
// and it has a 50% chance of erroring out.
async function longFunc(num) {
console.log(" ");
console.log(`Starting long running function number ${num}...`);
await sleep(2500);
// 50% chance of throwing an error
const rnd = Math.random();
if (rnd > 0.5) throw new Error(`An error was randomly generated.`);
await sleep(2500);
console.log(`...long running function finished.`);
console.log(" ");
}

This output was generated by the p-queue-retry.js program below. It illustrates how to setup a promise-based queue and auto-retry program using p-queue and p-retry

Starting long running function number 1...
queue.size: 2
Attempt 1 failed. There are 5 retries left.
Waiting 2 seconds before trying again.
 
Starting long running function number 1...
...long running function finished.
 
 
Starting long running function number 2...
...long running function finished.
 
 
Starting long running function number 3...
Attempt 1 failed. There are 5 retries left.
Waiting 2 seconds before trying again.
 
Starting long running function number 3...
Attempt 2 failed. There are 4 retries left.
Waiting 2 seconds before trying again.
 
Starting long running function number 3...
...long running function finished.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment