flash
cf71bab92d
This has been in the works for over a month and might break things because it's a very radical change. If it causes you to be unable to join chat, report it on the forum or try joining using the legacy chat on https://sockchat.flashii.net.
49 lines
1.1 KiB
JavaScript
49 lines
1.1 KiB
JavaScript
const TimedPromise = function(resolve, reject, always, timeoutMs) {
|
|
let timeout, resolved = false;
|
|
|
|
const cancelTimeout = () => {
|
|
if(timeout === undefined) {
|
|
clearTimeout(timeout);
|
|
timeout = undefined;
|
|
}
|
|
};
|
|
|
|
const doResolve = (...args) => {
|
|
if(resolved) return;
|
|
resolved = true;
|
|
|
|
cancelTimeout();
|
|
reject = undefined;
|
|
|
|
if(typeof resolve === 'function')
|
|
resolve(...args);
|
|
if(typeof always === 'function')
|
|
always();
|
|
};
|
|
|
|
const doReject = (...args) => {
|
|
if(resolved) return;
|
|
resolved = true;
|
|
|
|
cancelTimeout();
|
|
resolve = undefined;
|
|
|
|
if(typeof reject === 'function')
|
|
reject(...args);
|
|
if(typeof always === 'function')
|
|
always();
|
|
};
|
|
|
|
timeout = setTimeout(() => doReject('timeout'), timeoutMs);
|
|
|
|
return {
|
|
resolve: doResolve,
|
|
reject: doReject,
|
|
cancel: () => {
|
|
if(timeout === undefined)
|
|
return;
|
|
doReject('timeout');
|
|
},
|
|
};
|
|
};
|