made with esnextbin
Last active
November 17, 2016 13:35
-
-
Save briancavalier/b923d3944931266d7d850d35c26ede8a to your computer and use it in GitHub Desktop.
esnextbin sketch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "esnextbin-sketch", | |
"version": "0.0.0", | |
"dependencies": { | |
"most": "1.0.5" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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