Skip to content

Instantly share code, notes, and snippets.

@briancavalier
Last active November 17, 2016 13:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save briancavalier/b923d3944931266d7d850d35c26ede8a to your computer and use it in GitHub Desktop.
Save briancavalier/b923d3944931266d7d850d35c26ede8a to your computer and use it in GitHub Desktop.
esnextbin sketch
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>ESNextbin Sketch</title>
<!-- put additional styles and scripts here -->
</head>
<body>
<!-- put markup and other contents here -->
</body>
</html>
import { just, periodic, throwError, recoverWith, delay, join } from 'most'
// Given a stream, return a new stream that retries
// the original after an ms delay. Each subsequent failure
// will back off by doubling the previous ms
const retry = (ms, s) =>
recoverWith(() => backoff(ms, s), s)
const backoff = (ms, s) =>
retry(ms * 2, join(delay(ms, just(s))))
// Create a test stream that fails some number
// of times before finally succeeding
let attempts = 0
const failures = 5
const s = periodic(1000, 'trying')
.timestamp()
.tap(x => console.log(x))
.chain(x => ++attempts < failures ? throwError(x) : just(x))
// Try it out
retry(100, s)
.observe(x => console.log('here', x))
{
"name": "esnextbin-sketch",
"version": "0.0.0",
"dependencies": {
"most": "1.0.5"
}
}
'use strict';
var _most = require('most');
// Given a stream, return a new stream that retries
// the original after an ms delay. Each subsequent failure
// will back off by doubling the previous ms
var retry = function retry(ms, s) {
return (0, _most.recoverWith)(function () {
return backoff(ms, s);
}, s);
};
var backoff = function backoff(ms, s) {
return retry(ms * 2, (0, _most.join)((0, _most.delay)(ms, (0, _most.just)(s))));
};
// Create a test stream that fails some number
// of times before finally succeeding
var attempts = 0;
var failures = 5;
var s = (0, _most.periodic)(1000, 'trying').timestamp().tap(function (x) {
return console.log(x);
}).chain(function (x) {
return ++attempts < failures ? (0, _most.throwError)(x) : (0, _most.just)(x);
});
// Try it out
retry(100, s).observe(function (x) {
return console.log('here', x);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment