diff --git a/src/client.js b/src/client.js index 187944420..8af841c99 100644 --- a/src/client.js +++ b/src/client.js @@ -19,6 +19,7 @@ limitations under the License. "use strict"; const PushProcessor = require('./pushprocessor'); +import {sleep} from './utils'; /** * This is an internal module. See {@link MatrixClient} for the public class. @@ -3221,7 +3222,7 @@ MatrixClient.prototype.scrollback = function(room, limit, callback) { const self = this; // wait for a time before doing this request // (which may be 0 in order not to special case the code paths) - Promise.delay(timeToWaitMs).then(function() { + sleep(timeToWaitMs).then(function() { return self._createMessagesRequest( room.roomId, room.oldState.paginationToken, diff --git a/src/crypto/DeviceList.js b/src/crypto/DeviceList.js index 94b3140b0..27b1a23e4 100644 --- a/src/crypto/DeviceList.js +++ b/src/crypto/DeviceList.js @@ -31,6 +31,7 @@ import DeviceInfo from './deviceinfo'; import {CrossSigningInfo} from './CrossSigning'; import olmlib from './olmlib'; import IndexedDBCryptoStore from './store/indexeddb-crypto-store'; +import {sleep} from '../utils'; /* State transition diagram for DeviceList._deviceTrackingStatus @@ -763,7 +764,7 @@ class DeviceListUpdateSerialiser { // this serves as an easy solution for now. let prom = Promise.resolve(); for (const userId of downloadUsers) { - prom = prom.delay(5).then(() => { + prom = prom.then(sleep(5)).then(() => { return this._processQueryResponseForUser( userId, dk[userId], { master: masterKeys[userId], diff --git a/src/crypto/index.js b/src/crypto/index.js index 11b450a8e..7b1dad314 100644 --- a/src/crypto/index.js +++ b/src/crypto/index.js @@ -49,6 +49,7 @@ import { newUnexpectedMessageError, newUnknownMethodError, } from './verification/Error'; +import {sleep} from '../utils'; const defaultVerificationMethods = { [ScanQRCode.NAME]: ScanQRCode, @@ -1834,7 +1835,7 @@ Crypto.prototype.scheduleKeyBackupSend = async function(maxDelay = 10000) { // requests from different clients hitting the server all at // the same time when a new key is sent const delay = Math.random() * maxDelay; - await Promise.delay(delay); + await sleep(delay); let numFailures = 0; // number of consecutive failures while (1) { if (!this.backupKey) { @@ -1868,7 +1869,7 @@ Crypto.prototype.scheduleKeyBackupSend = async function(maxDelay = 10000) { } if (numFailures) { // exponential backoff if we have failures - await Promise.delay(1000 * Math.pow(2, Math.min(numFailures - 1, 4))); + await sleep(1000 * Math.pow(2, Math.min(numFailures - 1, 4))); } } } finally { diff --git a/src/utils.js b/src/utils.js index b752299f0..8cab9fbb8 100644 --- a/src/utils.js +++ b/src/utils.js @@ -708,3 +708,8 @@ module.exports.ensureNoTrailingSlash = function(url) { return url; } }; + +// Returns a promise which resolves with a given value after the given number of ms +module.exports.sleep = (ms, value) => new Promise((resolve => { + setTimeout(resolve, ms, value); +}));