Commit 428f04ef authored by Laurent Wouters's avatar Laurent Wouters
Browse files

[fix] Support adding timeout to promises

parent fa40a82c3a83
......@@ -185,7 +185,7 @@ export function probeResourceWith(
}
};
resource.uris.forEach((uri: string) =>
probeResourceUri(uri)
withTimeout(probe(uri), 250)
.then((uri: string) => {
remaining.push(uri);
onFinished();
......@@ -196,6 +196,35 @@ export function probeResourceWith(
);
}
/**
* Wraps a promise in a new one with a timeout
* @param original The original promise
* @param wait The time to wait
*/
export function withTimeout<T>(original: Promise<T>, wait: number): Promise<T> {
return new Promise<T>(
(resolve: (result: T) => void, reject: (reason: any) => void) => {
let timeoutTriggered = false;
let onTimeout = () => {
timeoutTriggered = true;
reject("timeout");
};
let handle = setTimeout(onTimeout, wait);
original
.then((result: T) => {
if (timeoutTriggered) return;
clearTimeout(handle);
resolve(result);
})
.catch((reason: any) => {
if (timeoutTriggered) return;
clearTimeout(handle);
reject(reason);
});
}
);
}
/**
* Probes an URI for a resource to check whether it is valid
* @param uri An URI
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment