Skip to content

Instantly share code, notes, and snippets.

@nckg
Last active August 11, 2017 09:01
Show Gist options
  • Select an option

  • Save nckg/5ceaea5353a53c6152b8e1317fd9b0c8 to your computer and use it in GitHub Desktop.

Select an option

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
/**
* 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