
Security News
Socket Releases Free Certified Patches for Critical vm2 Sandbox Escape
A critical vm2 sandbox escape can allow untrusted JavaScript to break isolation and execute commands on the host Node.js process.
stubborn-utils
Advanced tools
A small collection of utilities for making functions somewhat resilient against errors.
A small collection of utilities for making functions somewhat resilient against errors.
npm install stubborn-utils
attemptifyAsyncThis function wraps an async function, and in case this wrapped function rejects then the error is automatically caught by the onError callback.
import {attemptifyAsync} from 'stubborn-utils';
// Let's create a wrapped function
const asyncFunction = async () => {
await someAsyncThing ();
if ( Math.random () > 0.5 ) {
throw new Error ( 'Unlucky' );
} else {
return 123;
}
};
const attemptifiedAsyncFunction = attemptifyAsync ( asyncFunction, {
onError: error => {
return -1;
}
});
const result = attemptifiedAsyncFunction (); // => Promise<123 | -1>
attemptifySyncThis function wraps a sync function, and in case this wrapped function throws then the error is automatically caught by the onError callback.
import {attemptifySync} from 'stubborn-utils';
// Let's create a wrapped function
const syncFunction = () => {
if ( Math.random () > 0.5 ) {
throw new Error ( 'Unlucky' );
} else {
return 123;
}
};
const attemptifiedSyncFunction = attemptifySync ( syncFunction, {
onError: error => {
return -1;
}
});
const result = attemptifiedSyncFunction (); // => 123 | -1
retryifyAsyncThis function wraps an async function, and in case this wrapped function rejects then the isRetriable callback is called to decide if we should retry calling the function again.
There's also another layer of options before you can actually call the retryified function, that allows you to provide a maximum timeout for the retry loop, and an optional interval that should approximately pass between retries.
Before the function is retried again a random amount of milliseconds between 0 and interval will be waited for.
By default interval would be set to 250.
import {retryifyAsync} from 'stubborn-utils';
// Let's create a wrapped function
const asyncFunction = async () => {
await someAsyncThing ();
if ( Math.random () > 0.5 ) {
throw new Error ( 'Unlucky' );
} else {
return 123;
}
};
const retryifiedAsyncFunction = retryifyAsync ( asyncFunction, {
isRetriable: error => {
return true; // Always retriablein this scenario
}
});
const result = retryifiedAsyncFunction ({
timeout: 1_000,
interval: 100
}); // => Promise<123 | -1>, but Promise<123> with much higher probability
retryifySyncThis function wraps a sync function, and in case this wrapped function throws then the isRetriable callback is called to decide if we should retry calling the function again.
There's also another layer of options before you can actually call the retryified function, that allows you to provide a maximum timeout for the retry loop, and an optional interval that should approximately pass between retries.
Before the function is retried again a random amount of milliseconds between 0 and interval will be waited for.
By default interval would be set to 250.
import {retryifySync} from 'stubborn-utils';
// Let's create a wrapped function
const syncFunction = () => {
if ( Math.random () > 0.5 ) {
throw new Error ( 'Unlucky' );
} else {
return 123;
}
};
const retryifiedSyncFunction = retryifySync ( syncFunction, {
isRetriable: error => {
return true; // Always retriablein this scenario
}
});
const result = retryifiedSyncFunction ({
timeout: 1_000,
interval: 100
}); // => 123 | -1, but 123 with much higher probability
MIT © Fabio Spampinato
FAQs
A small collection of utilities for making functions somewhat resilient against errors.
The npm package stubborn-utils receives a total of 3,336,979 weekly downloads. As such, stubborn-utils popularity was classified as popular.
We found that stubborn-utils demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
A critical vm2 sandbox escape can allow untrusted JavaScript to break isolation and execute commands on the host Node.js process.

Research
Five malicious NuGet packages impersonate Chinese .NET libraries to deploy a stealer targeting browser credentials, crypto wallets, SSH keys, and local files.

Security News
pnpm 11 turns on a 1-day Minimum Release Age and blocks exotic subdeps by default, adding safeguards against fast-moving supply chain attacks.