Last active
August 11, 2017 09:01
-
-
Save nckg/5ceaea5353a53c6152b8e1317fd9b0c8 to your computer and use it in GitHub Desktop.
Extends Promise with a new property. Tells the Promise to take at least a minimum of given time before returning it
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
| /** | |
| * Extends Promise with a new property. Tells the Promise to take at least a minimum of given time. | |
| * | |
| * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/prototype | |
| * | |
| * Example: | |
| * | |
| * var p = new Promise(function (resolve, reject) { ... }); | |
| * p.takeAtLeast(1000).then(function () { ... }); | |
| */ | |
| Promise.prototype.takeAtLeast = function (ms) { | |
| var wait = new Promise(function (resolve) { | |
| setTimeout(function () { | |
| resolve(); | |
| }, ms); | |
| }); | |
| return Promise.all([this, wait]).then(function (p) { | |
| return p[0]; | |
| }); | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment