Security News
Bun 1.2 Released with 90% Node.js Compatibility and Built-in S3 Object Support
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.
@codeo/eventually
Advanced tools
Retry async operations with back-off, handle errors by halting, suppressing or failing
Retry async operations with back-off, handle errors by halting, suppressing or failing.
Easily retry an async function up to 3 times, throwing if the last attempt fails
import { eventually } from "@codeo/eventually";
const result = await eventually(() => queryApi(1, 2, 3), { retries: 3 });
Retry an async function with a back-off strategy: the passed array is a set of millisecond values to back off by. Once the last one is reached, that value is used for any further delays to retry attempts. If the following code were to fail all 5 times, it would back off by 50ms on the second attempt, 250ms on the third attempts, 1 second on the fourth and fifth attempts.
import { eventually } from "@codeo/eventually";
const result = await eventually(() => queryApi("user input"), {
retries: 5,
backoff: [ 50, 250, 1000 ]
});
backoff
may also be specified as a single numeric value to use for all attempts.
Halt on error: the promise never resolves. Useful for, eg, polling code where it's ok for an occasional failure or where failures are simply non-fatal, such as polling for an unread message count, which can fail when your app is backgrounded.
import { eventually, ErrorHandlingStrategies } from "@codeo/eventually";
const result = await eventually(() => poll("message-count"), {
retries: 2,
fail: async(e) => {
return ErrorHandlingStrategies.halt;
}
});
// if the above fails completely, then this part of code is never reached
console.log(`you have ${result} messages`);
Suppress an error: in this case, the caller continues with execution (will not
halt the promise chain or cause an await to "deadlock") and undefined
is returned
to the caller. Since fail
is an async function, you may choose how to proceed based on the
error you've received. Possible strategies are:
import { eventually, ErrorHandlingStrategies } from "@codeo/eventually";
const result = await eventually(() => willFail(), {
retries: 1,
fail: async(e: Error) => {
return ErrorHandlingStrategies.suppress;
}
});
Your fail
handler may even return a new IEventuallyOptions
object to
completely change the flow of logic
In addition, you may redirect errors with the redirect
property on the provided
options, for instance when you have a valid value to fall back on when your async
function fails:
import { eventually } from "@codeo/eventually";
const fallbackValue = 1;
const result = await eventually(pollServer, {
redirect: (e: Error) => fallbackValue
});
which you could use to query a secondary api eg, when the first is too loaded:
import { eventually } from "@codeo/eventually";
const fallbackValue = 1;
const result = await eventually(pollServer1, {
redirect: (e: Error) => eventually(pollServer2, {
redirect: (e: Error) => fallbackValue
})
});
FAQs
Retry async operations with back-off, handle errors by halting, suppressing or failing
The npm package @codeo/eventually receives a total of 34 weekly downloads. As such, @codeo/eventually popularity was classified as not popular.
We found that @codeo/eventually demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 3 open source maintainers 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
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.
Security News
Biden's executive order pushes for AI-driven cybersecurity, software supply chain transparency, and stronger protections for federal and open source systems.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.