Convert utils/promiseTimeout to TS

Reviewed By: danielbuechele

Differential Revision: D16710520

fbshipit-source-id: 146ec33537de038d59e6f13647ee0de7b9edbcb8
This commit is contained in:
John Knox
2019-08-12 03:02:16 -07:00
committed by Facebook Github Bot
parent aab92446b4
commit fda506086f
3 changed files with 5 additions and 5 deletions

View 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]);
}