re-add and actually use promise timeout util

Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>
pull/21833/head
Michael Telatynski 2019-11-14 09:37:26 +00:00
parent bc90789c71
commit 5c24547ef5
2 changed files with 18 additions and 7 deletions

View File

@ -26,6 +26,7 @@ import { getThreepidsWithBindStatus } from '../../../boundThreepids';
import IdentityAuthClient from "../../../IdentityAuthClient"; import IdentityAuthClient from "../../../IdentityAuthClient";
import {abbreviateUrl, unabbreviateUrl} from "../../../utils/UrlUtils"; import {abbreviateUrl, unabbreviateUrl} from "../../../utils/UrlUtils";
import { getDefaultIdentityServerUrl, doesIdentityServerHaveTerms } from '../../../utils/IdentityServerUtils'; import { getDefaultIdentityServerUrl, doesIdentityServerHaveTerms } from '../../../utils/IdentityServerUtils';
import {timeout} from "../../../utils/promise";
// We'll wait up to this long when checking for 3PID bindings on the IS. // We'll wait up to this long when checking for 3PID bindings on the IS.
const REACHABILITY_TIMEOUT = 10000; // ms const REACHABILITY_TIMEOUT = 10000; // ms
@ -245,14 +246,11 @@ export default class SetIdServer extends React.Component {
let threepids = []; let threepids = [];
let currentServerReachable = true; let currentServerReachable = true;
try { try {
threepids = await Promise.race([ threepids = await timeout(
getThreepidsWithBindStatus(MatrixClientPeg.get()), getThreepidsWithBindStatus(MatrixClientPeg.get()),
new Promise((resolve, reject) => { Promise.reject(new Error("Timeout attempting to reach identity server")),
setTimeout(() => { REACHABILITY_TIMEOUT,
reject(new Error("Timeout attempting to reach identity server")); );
}, REACHABILITY_TIMEOUT);
}),
]);
} catch (e) { } catch (e) {
currentServerReachable = false; currentServerReachable = false;
console.warn( console.warn(

View File

@ -22,6 +22,19 @@ import Promise from "bluebird";
// Returns a promise which resolves with a given value after the given number of ms // Returns a promise which resolves with a given value after the given number of ms
export const sleep = (ms: number, value: any): Promise => new Promise((resolve => { setTimeout(resolve, ms, value); })); export const sleep = (ms: number, value: any): Promise => new Promise((resolve => { setTimeout(resolve, ms, value); }));
// Returns a promise which resolves when the input promise resolves with its value
// or when the timeout of ms is reached with the value of given timeoutValue
export async function timeout(promise: Promise, timeoutValue: any, ms: number): Promise {
const timeoutPromise = new Promise((resolve) => {
const timeoutId = setTimeout(resolve, ms, timeoutValue);
promise.then(() => {
clearTimeout(timeoutId);
});
});
return Promise.race([promise, timeoutPromise]);
}
// Returns a Deferred // Returns a Deferred
export function defer(): {resolve: () => {}, reject: () => {}, promise: Promise} { export function defer(): {resolve: () => {}, reject: () => {}, promise: Promise} {
let resolve; let resolve;