Files
flipper/src/utils/promiseTimeout.tsx
Pritesh Nandgaonkar 5bdba4935a Commit hash field added
Summary:
This diff adds commit hash field in the support form v2 and we prefill the information.

If changes are not pushed to phabricator we will populate it with commit hash or else we will populate it with Diff number.

See the demo to understand the flow.

{F222000517}

Reviewed By: jknoxville

Differential Revision: D18427044

fbshipit-source-id: 80a58baca381e21203da5670e29144a7e8c2eeed
2019-11-12 04:52:42 -08:00

48 lines
1.2 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: 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]);
}
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;
});
}