Skip to content

Instantly share code, notes, and snippets.

@ktilcu
Created April 23, 2019 17:07
Show Gist options
  • Save ktilcu/87745b6de457295022e2454e15c237a6 to your computer and use it in GitHub Desktop.
Save ktilcu/87745b6de457295022e2454e15c237a6 to your computer and use it in GitHub Desktop.
Promise Retries // source https://jsbin.com/gorumid
<!DOCTYPE html>
<html>
<head>
<meta name="description" content="Promise Retries">
<script src="http://cdn.jsdelivr.net/bluebird/3.5.0/bluebird.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.22.1/ramda.min.js"></script>
</head>
<body>
<script id="jsbin-javascript">
'use strict';
function retry(fx, args, number) {
var attempt = fx.apply(null, args);
var errors = [];
return attempt.reflect().then(function (inspection) {
if (inspection.isFulfilled()) return inspection.value();
errors.push(inspection.reason());
if (number - 1 === 0) return Promise.reject(new Error(errors.join(',')));
return retry(fx, args, number - 1);
});
}
function settle(promises) {
return Promise.all(promises.map(function (promise) {
return promise.reflect();
}));
}
function reflect(promise) {
var x = {
isFulfilled: false,
isRejected: false,
value: undefined,
error: undefined
};
return promise['catch'](function (e) {
var out = Object.assign({}, x);
out.isRejected = true;
out.error = e;
return out;
}).then(function (v) {
var out = Object.assign({}, x);
out.isFulfilled = true;
out.value = v;
return out;
});
}
function randomFail(a) {
return Math.random() > .8 ? Promise.resolve('yessss!!') : Promise.reject(new Error('noooooo :('));
}
var omga = retry(randomFail, [1], 5).then(function (x) {
return console.log(x);
})['catch'](function (err) {
console.log('Error! ', err);
});
//var omgz = settle([omga, retry(randomFail, [1], 2), retry(randomFail, [1], 8)]);
// Promise.all(omgz)
// // .then(i => console.log(i))
// var successes = omgz
// .filter(p => p.isFulfilled())
// .map(i => i.value())
// .tap(console.log)
// var failures = omgz
// .filter(p => p.isRejected())
// .map(i => i.reason())
// .tap(console.log)
</script>
<script id="jsbin-source-html" type="text/html"><!DOCTYPE html>
<html>
<head>
<meta name="description" content="Promise Retries">
<script src="//cdn.jsdelivr.net/bluebird/3.5.0/bluebird.js"><\/script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.22.1/ramda.min.js"><\/script>
</head>
<body>
</body>
</html></script>
<script id="jsbin-source-javascript" type="text/javascript">function retry (fx, args, number) {
var attempt = fx.apply(null, args);
var errors = [];
return attempt
.reflect()
.then(function (inspection) {
if (inspection.isFulfilled()) return inspection.value();
errors.push(inspection.reason());
if (number - 1 === 0) return Promise.reject(new Error(errors.join(',')));
return retry(fx, args, number -1);
});
}
function settle (promises) {
return Promise.all(promises.map(promise => promise.reflect()));
}
function reflect (promise) {
var x = {
isFulfilled: false,
isRejected: false,
value: undefined,
error: undefined
};
return promise
.catch(e => {
var out = Object.assign({}, x);
out.isRejected = true;
out.error = e;
return out;
})
.then(v => {
var out = Object.assign({}, x);
out.isFulfilled = true;
out.value = v;
return out;
})
}
function randomFail (a) {
return Math.random() >.8 ? Promise.resolve('yessss!!') : Promise.reject(new Error('noooooo :('));
}
var omga = retry(randomFail, [1], 5)
.then(x=>console.log(x))
.catch(function(err){
console.log('Error! ', err);
});
//var omgz = settle([omga, retry(randomFail, [1], 2), retry(randomFail, [1], 8)]);
// Promise.all(omgz)
// // .then(i => console.log(i))
// var successes = omgz
// .filter(p => p.isFulfilled())
// .map(i => i.value())
// .tap(console.log)
// var failures = omgz
// .filter(p => p.isRejected())
// .map(i => i.reason())
// .tap(console.log)
</script></body>
</html>
'use strict';
function retry(fx, args, number) {
var attempt = fx.apply(null, args);
var errors = [];
return attempt.reflect().then(function (inspection) {
if (inspection.isFulfilled()) return inspection.value();
errors.push(inspection.reason());
if (number - 1 === 0) return Promise.reject(new Error(errors.join(',')));
return retry(fx, args, number - 1);
});
}
function settle(promises) {
return Promise.all(promises.map(function (promise) {
return promise.reflect();
}));
}
function reflect(promise) {
var x = {
isFulfilled: false,
isRejected: false,
value: undefined,
error: undefined
};
return promise['catch'](function (e) {
var out = Object.assign({}, x);
out.isRejected = true;
out.error = e;
return out;
}).then(function (v) {
var out = Object.assign({}, x);
out.isFulfilled = true;
out.value = v;
return out;
});
}
function randomFail(a) {
return Math.random() > .8 ? Promise.resolve('yessss!!') : Promise.reject(new Error('noooooo :('));
}
var omga = retry(randomFail, [1], 5).then(function (x) {
return console.log(x);
})['catch'](function (err) {
console.log('Error! ', err);
});
//var omgz = settle([omga, retry(randomFail, [1], 2), retry(randomFail, [1], 8)]);
// Promise.all(omgz)
// // .then(i => console.log(i))
// var successes = omgz
// .filter(p => p.isFulfilled())
// .map(i => i.value())
// .tap(console.log)
// var failures = omgz
// .filter(p => p.isRejected())
// .map(i => i.reason())
// .tap(console.log)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment