"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
function promiseTimeout(ms, promise, errorFactory) {
    if (errorFactory === void 0) { errorFactory = function () { return new Error('Timeout error'); }; }
    var startTime = new Date();
    if (ms <= 0) {
        var error = errorFactory({
            timeout: ms,
            startTime: startTime
        });
        return Promise.reject(error);
    }
    // Create a promise that rejects in <ms> milliseconds
    var timeout = new Promise(function (resolve, reject) {
        var id = setTimeout(function () {
            clearTimeout(id);
            var error = errorFactory({
                timeout: ms,
                startTime: startTime
            });
            reject(error);
        }, ms);
    });
    // Returns a race between our timeout and the passed in promise
    return Promise.race([
        promise,
        timeout
    ]);
}
exports.promiseTimeout = promiseTimeout;
