I need the promise to resolve within the specified time or reject it. I'm doing this:
function myFunction(url, maxWait) {
maxWait = maxWait || 5000; // milliseconds
var promise = new Promise(function(resolve, reject) {
$.ajax({
url: url,
})
.done(function(data) {
resolve(data);
});
});
var timeout = new Promise(function(resolve, reject) {
var id = setTimeout(function() {
clearTimeout(id);
reject(Error("Timed out"));
}, maxWait);
});
return Promise.race([promise, timeout]);
}
... but the timeout always runs, what am I doing wrong? Basically I need to set a timeout on the promise so it resolves or rejects within maxWait
ms.