UNPKG

prex-es5

Version:

Async coordination primitives and extensions on top of ES6 Promises

49 lines (46 loc) 1.51 kB
/*! ***************************************************************************** Copyright (c) Microsoft Corporation. Licensed under the Apache License, Version 2.0. See LICENSE file in the project root for details. ***************************************************************************** */ import { CancellationToken } from "./cancellation"; /** * Waits the specified number of milliseconds before resolving with the provided value. * * @param token A CancellationToken * @param msec The number of milliseconds to wait before resolving. * @param value An optional value for the resulting Promise. */ export function delay(token_, msec_, value) { let token; let msec; if (typeof token_ === "number") { value = msec_; msec = token_; token = CancellationToken.none; } else { msec = msec_; token = token_; } if (!token.canBeCanceled) { return new Promise(resolve => setTimeout(resolve, msec, value)); } return new Promise((resolve, reject) => { token.throwIfCancellationRequested(); const handle = setTimeout(() => { registration.unregister(); resolve(value); }, msec); const registration = token.register(() => { clearTimeout(handle); try { token.throwIfCancellationRequested(); } catch (e) { reject(e); } }); }); } //# sourceMappingURL=delay.js.map