Convert utils/promiseTimeout to TS
Reviewed By: danielbuechele Differential Revision: D16710520 fbshipit-source-id: 146ec33537de038d59e6f13647ee0de7b9edbcb8
This commit is contained in:
committed by
Facebook Github Bot
parent
aab92446b4
commit
fda506086f
23
src/utils/promiseTimeout.tsx
Normal file
23
src/utils/promiseTimeout.tsx
Normal file
@@ -0,0 +1,23 @@
|
||||
/**
|
||||
* Copyright 2018-present Facebook.
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
* @format
|
||||
*/
|
||||
|
||||
export default function promiseTimeout<T>(
|
||||
ms: number,
|
||||
promise: Promise<T>,
|
||||
timeoutMessage?: string,
|
||||
): Promise<T> {
|
||||
// Create a promise that rejects in <ms> milliseconds
|
||||
const timeout: Promise<T> = new Promise((resolve, reject) => {
|
||||
const id = setTimeout(() => {
|
||||
clearTimeout(id);
|
||||
reject(new Error(timeoutMessage || `Timed out in ${ms} ms.`));
|
||||
}, ms);
|
||||
});
|
||||
|
||||
// Returns a race between our timeout and the passed in promise
|
||||
return Promise.race([promise, timeout]);
|
||||
}
|
||||
Reference in New Issue
Block a user