Files
flipper/src/utils/promiseTimeout.tsx
Alexander Putilin c7af0b53e6 KaiOS device plugin for recording memory allocations
Summary:
This adds basic plugin for recording memory allocations.
{F224005997}

Reviewed By: passy

Differential Revision: D18837209

fbshipit-source-id: aa29680b3a44c51135b760fe4ee76edc6d0ec109
2019-12-12 08:12:52 -08:00

54 lines
1.3 KiB
TypeScript

/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
*/
import {StatusMessageType} from '../reducers/application';
export default function promiseTimeout<T>(
ms: number,
promise: Promise<T>,
timeoutMessage?: string,
): Promise<T> {
// Create a promise that rejects in <ms> milliseconds
const timeout = sleep(ms).then(() => {
throw new Error(timeoutMessage || `Timed out in ${ms} ms.`);
});
// Returns a race between our timeout and the passed in promise
return Promise.race([promise, timeout]);
}
export function sleep(ms: number): Promise<void> {
return new Promise(resolve => {
const id = setTimeout(() => {
clearTimeout(id);
resolve();
}, ms);
});
}
export function showStatusUpdatesForPromise<T>(
promise: Promise<T>,
message: string,
sender: string,
addStatusMessage: (payload: StatusMessageType) => void,
removeStatusMessage: (payload: StatusMessageType) => void,
): Promise<T> {
const statusMsg = {msg: message, sender};
addStatusMessage(statusMsg);
return promise
.then(result => {
removeStatusMessage(statusMsg);
return result;
})
.catch(e => {
removeStatusMessage(statusMsg);
throw e;
});
}