made with requirebin
Created
December 15, 2017 10:31
-
-
Save PeterHancock/63c0fff13560d47066771fca1301ae19 to your computer and use it in GitHub Desktop.
requirebin sketch
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
// Welcome! require() some modules from npm (like you were using browserify) | |
// and then hit Run Code to run your code on the right side. | |
// Modules get downloaded from browserify-cdn and bundled in your browser. | |
const delay = (t) => new Promise((resolve, reject) => { | |
setTimeout(() => resolve(delay.DELAY), t) | |
}) | |
delay.DELAY = Symbol('DELAY') | |
const timeout = (t) => (p) => Promise.race([p, delay(t)]).then( | |
(value) => { | |
console.log('timeout?', value === delay.DELAY) | |
if (value === delay.DELAY) { | |
throw 'Operation timed out' | |
} | |
return value | |
} | |
) | |
const retry = (n) => (f) => new Promise((resolve, reject) => { | |
const attempt = (n) => { | |
if (n > 0) { | |
f().then(resolve).catch(() => attempt(n - 1)) | |
} else { | |
return reject(retry.FAILED) | |
} | |
} | |
attempt(n + 1) | |
}) | |
retry.FAILED = Symbol('RETRY_FAILED') | |
const getStuff = (...$) => delay(100).then(() => [...$]) | |
getStuff(1, 2, 3) | |
.then((x) => console.log('success 1', x)) | |
.catch((x) => console.error('Failure 1', x)); | |
( | |
(...$) => timeout(20)(getStuff(...$)) | |
)(1, 2, 3) | |
.catch((x) => console.error('Failure', x)); | |
( | |
(...$) => retry(2)(() => getStuff(...$)) | |
)(1, 2, 3) | |
.then((x) => console.error('Success 2', x)) | |
.catch((x) => console.error('Failure 2', x)); | |
( | |
(...$) => retry(2)(() => timeout(20)(getStuff(...$))) | |
)(1, 2, 3) | |
.then((x) => console.error('Success 3', x)) | |
.catch((x) => console.error('Failure 3', x)); | |
// (...(() => P) => () => P) => (() => P) => () => P | |
const compose = (...promiseFns) => promiseFns.reduce((acc, promiseFn) => (f) => acc(promiseFn(f))) | |
const retryTwiceWithTimeout = compose( | |
(f) => () => retry(2)(f), | |
(f) => () => timeout(20)(f()) | |
) | |
const getStuffSafe = (...$) => retryTwiceWithTimeout(() => getStuff(...$))() | |
getStuffSafe(1, 2, 3) | |
.then((x) => console.log('success compose', x)) | |
.catch((x) => console.error('Failure compose', x)) | |
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
setTimeout(function(){ | |
;// Welcome! require() some modules from npm (like you were using browserify) | |
// and then hit Run Code to run your code on the right side. | |
// Modules get downloaded from browserify-cdn and bundled in your browser. | |
const delay = (t) => new Promise((resolve, reject) => { | |
setTimeout(() => resolve(delay.DELAY), t) | |
}) | |
delay.DELAY = Symbol('DELAY') | |
const timeout = (t) => (p) => Promise.race([p, delay(t)]).then( | |
(value) => { | |
console.log('timeout?', value === delay.DELAY) | |
if (value === delay.DELAY) { | |
throw 'Operation timed out' | |
} | |
return value | |
} | |
) | |
const retry = (n) => (f) => new Promise((resolve, reject) => { | |
const attempt = (n) => { | |
if (n > 0) { | |
f().then(resolve).catch(() => attempt(n - 1)) | |
} else { | |
return reject(retry.FAILED) | |
} | |
} | |
attempt(n + 1) | |
}) | |
retry.FAILED = Symbol('RETRY_FAILED') | |
const getStuff = (...$) => delay(100).then(() => [...$]) | |
getStuff(1, 2, 3) | |
.then((x) => console.log('success 1', x)) | |
.catch((x) => console.error('Failure 1', x)); | |
( | |
(...$) => timeout(20)(getStuff(...$)) | |
)(1, 2, 3) | |
.catch((x) => console.error('Failure', x)); | |
( | |
(...$) => retry(2)(() => getStuff(...$)) | |
)(1, 2, 3) | |
.then((x) => console.error('Success 2', x)) | |
.catch((x) => console.error('Failure 2', x)); | |
( | |
(...$) => retry(2)(() => timeout(20)(getStuff(...$))) | |
)(1, 2, 3) | |
.then((x) => console.error('Success 3', x)) | |
.catch((x) => console.error('Failure 3', x)); | |
// (...(() => P) => () => P) => (() => P) => () => P | |
const compose = (...promiseFns) => promiseFns.reduce((acc, promiseFn) => (f) => acc(promiseFn(f))) | |
const retryTwiceWithTimeout = compose( | |
(f) => () => retry(2)(f), | |
(f) => () => timeout(20)(f()) | |
) | |
const getStuffSafe = (...$) => retryTwiceWithTimeout(() => getStuff(...$))() | |
getStuffSafe(1, 2, 3) | |
.then((x) => console.log('success compose', x)) | |
.catch((x) => console.error('Failure compose', x)) | |
;}, 0) |
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
{ | |
"name": "requirebin-sketch", | |
"version": "1.0.0" | |
} |
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
<!-- contents of this file will be placed inside the <body> --> |
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
<!-- contents of this file will be placed inside the <head> --> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment