Created
December 18, 2016 17:36
-
-
Save rpbeltran/26679dc3beb5d619c46ed69d3a35bf8a to your computer and use it in GitHub Desktop.
Times Promise
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
// Times Promise | |
// By: Ryan Beltran | |
function times(t) { | |
return new Promise(function (resolve) { | |
for (var i = 0; i < t; i++) { | |
resolve(); | |
} | |
}); | |
} | |
Promise.times = function (fn, t) { | |
if (!t) { | |
t = fn; | |
fn = function () {}; | |
} | |
var promise = times(t); | |
for (var i = 0; i < t; i++) { | |
promise = promise.then(fn); | |
} | |
return promise; | |
} | |
Promise.prototype.times = function (fn, t) { | |
return this.then(function () { | |
return Promise.times(fn, t); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example Usage: