Promisify a function call with timeouts
I have seen many resources provide similar examples of using Promise.race
to timeout a function call within a given period of time. This is a very good example of how Promise.race
can be used in practice. Here's some sample code:
function doWithinInterval(func, timeout) {
var promiseTimeout = new Promise(function (fulfill, reject) {
// Rejects as soon as the timeout kicks in
setTimeout(reject, timeout);
});
var promiseFunc = new Promise(function (fulfill, reject) {
var result = func(); // Function that may take long to finish
// Fulfills when the given function finishes
fulfill(result);
});
return Promise.race([promiseTimeout, promiseFunc]);
}
The simple approach above using Promise.race
rejects the promise as soon as the timeout kicks in before func
has completed. Otherwise, the project is fulfilled once the func
function finishes before the timeout interval.
This sounds good and easy to use.
However, is this the best practice to use timeout in Promise?
Surely, the approach above can be employed if we want to set a timeout against a function call using Promises. The operations still appear to be a good promise. However, is this considered a good practice of using timeout in a Promise? If not, what is the disadvantage of using this?
I've look for alternative approaches, but couldn't find a native Promise way to do this.
Instead, some external Promise libraries offer timeout
functionality as follows:
Bluebird supplies
.timeout()
WinJS supplies
.timeout()
as wellQ also comes with
.timeout()
.
However, Promise.timeout()
doesn't appear to be part of the standard ECMAScript 6 API (please correct me if I'm wrong). Is there any recommended way to handle timeouts natively with ES6 Promises?