1
0
mirror of https://github.com/matrix-org/matrix-js-sdk.git synced 2025-11-25 05:23:13 +03:00

Add async setImmediate util

Adds an async/promise-based version of `setImmediate`. Note that, despite being
poorly adopted, `setImmediate` is polyfilled, and should be more performant
than `sleep(0)`.

Signed-off-by: Clark Fischer <clark.fischer@gmail.com>
This commit is contained in:
Clark Fischer
2023-01-16 07:35:23 -08:00
parent a34d06c7c2
commit ddce1bcd28
2 changed files with 45 additions and 3 deletions

View File

@@ -1,6 +1,5 @@
/*
Copyright 2015, 2016 OpenMarket Ltd
Copyright 2019 The Matrix.org Foundation C.I.C.
Copyright 2015, 2016, 2019, 2023 The Matrix.org Foundation C.I.C.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
@@ -392,13 +391,22 @@ export function ensureNoTrailingSlash(url?: string): string | undefined {
}
}
// 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 function sleep<T>(ms: number, value?: T): Promise<T> {
return new Promise((resolve) => {
setTimeout(resolve, ms, value);
});
}
/**
* Promise/async version of {@link setImmediate}.
*/
export function immediate(): Promise<void> {
return new Promise(setImmediate);
}
export function isNullOrUndefined(val: any): boolean {
return val === null || val === undefined;
}