Promise-based function retry utility. Designed for async/await
.
npm install recaller
example partially stolen from async-retry's example
import recaller, { constantBackoff } from "recaller";
import fetch from "node-fetch";
export default async function fetchSomething() {
return await recaller(
async (bail, attempt) => {
const res = await fetch("https://google.com");
if (403 === res.status) {
// we're not going to retry
return bail(new Error("Unauthorized"));
}
const data = await res.text();
return data.substr(0, 500);
},
{
// default: 2 retries
retries: 10,
// default: no backoff, retry immediately
backoff: constantBackoff(1000),
},
);
}
The code is fully TSDoc'd. See src/index.ts
for documentation of the main functions, listed below:
recaller(fn, opts)
constantBackoff(ms)
fullJitterBackoff(opts)
recaller
doesn't backoff (wait before retrying) by default. To specify backoff, you must give it a backoff function in the options (opts.backoff
).
example:
import recaller, { constantBackoff } from 'recaller'
export default function doSomething () {
return await recaller(async () => {
const res = await fetch('https://google.com')
}, {
// on every failure, wait 5 seconds before retrying
backoff: constantBackoff(5000)
})
}
A backoff function, given an attempt count, returns the next delay to wait in milliseconds.
For example, constantBackoff(ms)
below:
function constantBackoff(ms) {
ms = ms ?? 5000;
return (attempt) => ms;
}
recaller
comes with 5 backoff generator functions, inspired by AWS's exponential backoff blog post.
Use fullJitterBackoff
for most cases, as it generally gives you the best results. You only really have to tweak the base
and cap
with it. See code for more documentation.
constantBackoff(ms)
fullJitterBackoff({base, cap, factor})
the following aren't recommended, and only exist for completeness:
exponentialBackoff({base, cap, factor})
equalJitterBackoff({base, cap, factor})
decorrelatedJitterBackoff({base, cap, times})
You can intercept each retry attempt, by providing a function in opts.onretry
.
import recaller from 'recaller'
export default function doSomething () {
return await recaller(async () => {
const res = await fetch('https://google.com')
}, {
onretry: function (err, attempt, delayTime) {
// Prevent retries; reject the recaller with the last error
if (err instanceof TypeError) throw err
// err is the error of the attempt
// attempt is the attempt #. If the first call failed, then attempt = 1.
// delayTime is how long we will wait before next attempt.
logger.warn(`doSomething attempt ${attempt} failed;
will wait ${delayTime} ms before trying again.
error: ${err}
`)
}
})
}
recaller's People
Forkers
justforkinrecaller's Issues
feature: onretry delay time overriding
If onretry returns a number, then that's how long we wait instead.
An in-range update of rimraf is breaking the build 🚨
Version 2.6.2 of rimraf just got published.
Branch | Build failing 🚨 |
---|---|
Dependency | rimraf |
Current Version | 2.6.1 |
Type | devDependency |
This version is covered by your current version range and after updating it in your project the build failed.
As rimraf is “only” a devDependency of this project it might not break production or downstream projects, but “only” your build or test tools – preventing new deploys or publishes.
I recommend you give this issue a high priority. I’m sure you can resolve this 💪
Status Details
- ❌ continuous-integration/travis-ci/push The Travis CI build failed Details
Not sure how things should work exactly?
There is a collection of frequently asked questions and of course you may always ask my humans.
Your Greenkeeper Bot 🌴
100% coverage
help
An in-range update of coveralls is breaking the build 🚨
Version 2.12.0 of coveralls just got published.
Branch | Build failing 🚨 |
---|---|
Dependency | coveralls |
Current Version | 2.11.16 |
Type | devDependency |
This version is covered by your current version range and after updating it in your project the build failed.
As coveralls is “only” a devDependency of this project it might not break production or downstream projects, but “only” your build or test tools – preventing new deploys or publishes.
I recommend you give this issue a high priority. I’m sure you can resolve this 💪
Status Details
- ❌ continuous-integration/travis-ci/push The Travis CI build failed Details
Release Notes
Branch coverage supportAdds branch coverage data to Coveralls API post.
Not sure how things should work exactly?
There is a collection of frequently asked questions and of course you may always ask my humans.
Your Greenkeeper Bot 🌴
An in-range update of nyc is breaking the build 🚨
Version 10.2.0 of nyc just got published.
Branch | Build failing 🚨 |
---|---|
Dependency | nyc |
Current Version | 10.1.2 |
Type | devDependency |
This version is covered by your current version range and after updating it in your project the build failed.
As nyc is “only” a devDependency of this project it might not break production or downstream projects, but “only” your build or test tools – preventing new deploys or publishes.
I recommend you give this issue a high priority. I’m sure you can resolve this 💪
Status Details
- ❌ continuous-integration/travis-ci/push The Travis CI build failed Details
Commits
The new version differs by 6 commits .
455619f
chore(release): 10.2.0
95cc09a
feat: upgrade to version of yargs with extend support (#541)
43535f9
chore: explicit update of istanbuljs dependencies (#535)
98ebdff
feat: allow babel cache to be enabled (#517)
50adde4
feat: exclude the coverage/ folder by default 🚀 (#502)
6a59834
chore(package): update tap to version 10.0.0 (#507)
See the full diff.
Not sure how things should work exactly?
There is a collection of frequently asked questions and of course you may always ask my humans.
Your Greenkeeper Bot 🌴
setup greenkeeper
An in-range update of standard is breaking the build 🚨
Version 9.0.2 of standard just got published.
Branch | Build failing 🚨 |
---|---|
Dependency | standard |
Current Version | 9.0.1 |
Type | devDependency |
This version is covered by your current version range and after updating it in your project the build failed.
As standard is “only” a devDependency of this project it might not break production or downstream projects, but “only” your build or test tools – preventing new deploys or publishes.
I recommend you give this issue a high priority. I’m sure you can resolve this 💪
Status Details
- ❌ continuous-integration/travis-ci/push The Travis CI build failed Details
Commits
The new version differs by 4 commits .
ba16c40
9.0.2
07562b6
changelog
b1cf49e
[email protected]
211d8dd
[email protected]
See the full diff.
Not sure how things should work exactly?
There is a collection of frequently asked questions and of course you may always ask my humans.
Your Greenkeeper Bot 🌴
fix line
https://github.com/SEAPUNK/recaller/blob/master/lib/index.js#L93
too lazy to do it right now
0 deps
Maybe we should put mini-copromise and delay inside of the code?
At least delay, because delay is a bit bulky for what I use it for.
test onretry handling
and the error throwing
ES6 modules
have a jsnext:main field in package.json for the full ES6 stuff (modules, etc.)
for regular main, just have a file that essentially wraps around the transpiled file, doing the whole export how we're doing it right now
readme, tests, docs
help
An in-range update of babel-eslint is breaking the build 🚨
Version 7.2.0 of babel-eslint just got published.
Branch | Build failing 🚨 |
---|---|
Dependency | babel-eslint |
Current Version | 7.1.1 |
Type | devDependency |
This version is covered by your current version range and after updating it in your project the build failed.
As babel-eslint is “only” a devDependency of this project it might not break production or downstream projects, but “only” your build or test tools – preventing new deploys or publishes.
I recommend you give this issue a high priority. I’m sure you can resolve this 💪
Status Details
- ❌ continuous-integration/travis-ci/push The Travis CI build failed Details
Release Notes
v7.2.0New Feature
- Add option to disable code frame. (#446) (Luís Couto)
Main change is just an option to disable the codeframe (added in v7.1.1) for html output and more (thanks to @Couto).
{ "parser": "babel-eslint", "parserOptions": { "codeFrame": false }, "extends": "eslint:recommended" }
Bug Fix
- [flow] Process polymorphic type bounds on functions (#444) (Alex Rattray)
Internal/Docs
- Use
lodash
instead oflodash.pickby
. (#435) (wtgtybhertgeghgtwtg) - Updates ESLint version/remove unnecessary config (Kai Cataldo)
- Remove broken ESLint tests (Kai Cataldo)
- Upgrade outdated dependencies (Kai Cataldo)
- remove deprecated rule examples [skip ci] (Henry Zhu)
- update readme [skip ci] (Henry Zhu)
- chore(package): update eslint-config-babel to version 6.0.0 (#433) (Henry Zhu)
- Update to use Node 4 features (#425) (Nazim Hajidin)
- chore(package): update eslint-config-babel to version 4.0.0 (#430) (greenkeeper[bot])
- add badges [skip ci] (Henry Zhu)
- Revert "use
*
" (#426) (Henry Zhu) - use
*
(#421) (Henry Zhu) - chore(package): update eslint-config-babel to version 3.0.0 (#423) (greenkeeper[bot])
Commits
The new version differs by 17 commits .
4db4db5
7.2.0
4499412
Use
lodash
instead oflodash.pickby
. (#435)a2c3b30
[flow] Process polymorphic type bounds on functions (#444)
515adef
Add option to disable code frame. (#446)
ce66e73
Merge pull request #447 from kaicataldo/clean-up-eslint
b49ab20
Updates ESLint version/remove unnecessary config
702d6b8
Remove broken ESLint tests
6b4c4ca
Upgrade outdated dependencies
bdeb86f
remove deprecated rule examples [skip ci]
52b4a13
update readme [skip ci]
0e5aca3
chore(package): update eslint-config-babel to version 6.0.0 (#433)
781dc77
Update to use Node 4 features (#425)
265d219
chore(package): update eslint-config-babel to version 4.0.0 (#430)
e6af5c5
add badges [skip ci]
a91a9d0
Revert "use
*
" (#426)
There are 17 commits in total. See the full diff.
Not sure how things should work exactly?
There is a collection of frequently asked questions and of course you may always ask my humans.
Your Greenkeeper Bot 🌴
don't call it middleware
call it a hook
promise constructor antipattern
https://github.com/SEAPUNK/recaller/blob/master/lib/index.js#L44
you can just return this
mini-copromise -> minico
example: async
the functions are not async in the example
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.