UNPKG

toad-scheduler

Version:

In-memory Node.js and browser job scheduler

37 lines 1.22 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.AsyncTask = exports.isAsyncTask = void 0; const Logger_1 = require("./Logger"); const Utils_1 = require("./Utils"); function isAsyncTask(task) { return task.isAsync === true; } exports.isAsyncTask = isAsyncTask; class AsyncTask { constructor(id, handler, errorHandler) { this.isAsync = true; this.id = id; this.handler = handler; this.errorHandler = errorHandler || (0, Logger_1.defaultErrorHandler)(this.id); this.isExecuting = false; } execute(jobId) { void this.executeAsync(jobId); } executeAsync(jobId) { this.isExecuting = true; return this.handler(this.id, jobId) .catch((err) => { const errorHandleResult = this.errorHandler(err); if ((0, Utils_1.isPromise)(errorHandleResult)) { // If we fail while handling an error, oh well errorHandleResult.catch((0, Logger_1.loggingErrorHandler)(err)); } }) .finally(() => { this.isExecuting = false; }); } } exports.AsyncTask = AsyncTask; //# sourceMappingURL=AsyncTask.js.map