Skip to content

Instantly share code, notes, and snippets.

@bellbind
Created April 14, 2015 05:08
Show Gist options
  • Save bellbind/9ad35e3b7274e377ef65 to your computer and use it in GitHub Desktop.
Save bellbind/9ad35e3b7274e377ef65 to your computer and use it in GitHub Desktop.
[es6][promise]Promise utilities: wait and timeout
// Promise utilities: wait and timeout
var wait = function (ms) {
return function waiting(v) {
return new Promise(function (f, r) {setTimeout(f, ms, v);});
};
};
var timeout = function (ms) {
return new Promise(function (f, r) {setTimeout(r, ms, ms);});
};
// <<examples>>
// timeout promise
Promise.race([10, timeout(100)]).then(function (v) {
console.log("success", v);
}, function (e) {
console.log("timeout", e);
});
// wait promise
Promise.race([wait(50)(Promise.resolve(20)), timeout(100)]).then(function (v) {
console.log("success", v);
}, function (e) {
console.log("timeout", e);
});
// wait promise
Promise.race([
Promise.resolve(30).then(wait(50)), timeout(100)
]).then(function (v) {
console.log("success", v);
}, function (e) {
console.log("timeout", e);
});
// wait timeout
Promise.race([
wait(50)(Promise.resolve(40)), timeout(10)
]).then(function (v) {
console.log("success", v);
}, function (e) {
console.log("timeout", e);
});
// wait timeout
Promise.race([
Promise.resolve(50).then(wait(50)), timeout(20)
]).then(function (v) {
console.log("success", v);
}, function (e) {
console.log("timeout", e);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment