Created
April 13, 2018 14:28
-
-
Save ktilcu/49d878527400547e6a3f974d03d11eb0 to your computer and use it in GitHub Desktop.
Future URL fetching
This file contains hidden or 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
| const Future = require('fluture'); | |
| // (Number -> Number) -> Number -> Future a b -> Future (List a) b | |
| const retry = (time, max, task) => { | |
| const failures = new Array(max); | |
| return (function recurse(i) { | |
| return task.chainRej(function(failure) { | |
| failures[i] = failure; | |
| const total = i + 1; | |
| return total === max | |
| ? Future.reject(failures) | |
| : Future.after(time(total), total).chain(recurse); | |
| }); | |
| })(0); | |
| }; | |
| // Number -> Number -> Number | |
| const linearly = t => n => t * n; | |
| // Number -> Number -> Number | |
| const exponentially = t => n => t * Math.pow(n, 2); | |
| // getHtml :: Url -> Future Err Html | |
| const getHtml = url => retry(linearly(1), 3, requestF(url)); //requestF is just request-promise wrapped in a future | |
| // will retry starting at a 1 sec gap increasing linearly up to 3 times max | |
| getHtml('http://google.com') | |
| .map(doStuffToHTML) // if the site failed to fetch this is a noop | |
| .fork(console.error, console.log) // The Future doesn't actually execute until a `.fork` call is made |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment