@freemework/common
Version:
Common library of the Freemework Project.
36 lines • 1.34 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.FSleep = FSleep;
exports.fsleep = fsleep;
const index_js_1 = require("../cancellation/index.js");
const index_js_2 = require("../execution_context/index.js");
function FSleep(data, ms) {
return fsleep(data, ms);
}
function fsleep(data, ms) {
const cancellationToken = data instanceof index_js_2.FExecutionContext
? index_js_1.FCancellationExecutionContext.of(data).cancellationToken
: data;
return new Promise((resolve, reject) => {
if (cancellationToken.isCancellationRequested) {
return reject(new index_js_1.FCancellationException());
}
let timeout = null;
if (ms !== undefined) {
function timeoutCallback() {
cancellationToken.removeCancelListener(cancelCallback);
return resolve();
}
timeout = setTimeout(timeoutCallback, ms);
}
function cancelCallback() {
cancellationToken.removeCancelListener(cancelCallback);
if (timeout !== null) {
clearTimeout(timeout);
}
return reject(new index_js_1.FCancellationException());
}
cancellationToken.addCancelListener(cancelCallback);
});
}
//# sourceMappingURL=f_sleep.js.map