diff --git a/src/task.ts b/src/task.ts index 6413764..5d27fcf 100644 --- a/src/task.ts +++ b/src/task.ts @@ -98,7 +98,7 @@ export class Task implements Promise { | null | undefined ): Promise { - return this.execute().then(onfulfilled, onrejected); + return this.#execute().then(onfulfilled, onrejected); } catch( @@ -107,16 +107,16 @@ export class Task implements Promise { | null | undefined ): Promise { - return this.execute().catch(onrejected); + return this.#execute().catch(onrejected); } finally(onfinally?: (() => void) | null | undefined): Promise { - return this.execute().finally(onfinally); + return this.#execute().finally(onfinally); } - execute(): Promise { - this.#promise ??= this.#resolve(); - return this.#promise; + perform(): Task { + this.#execute(); + return this; } async cancel(cancelReason = "Task has been cancelled."): Promise { @@ -138,6 +138,11 @@ export class Task implements Promise { } } + #execute(): Promise { + this.#promise ??= this.#resolve(); + return this.#promise; + } + async #resolve(): Promise { // We need to check if the task has been cancelled before we start the promise. this.#abortController.signal.throwIfAborted(); diff --git a/tests/vitest/task.test.ts b/tests/vitest/task.test.ts index 45bb961..cf07203 100644 --- a/tests/vitest/task.test.ts +++ b/tests/vitest/task.test.ts @@ -7,14 +7,14 @@ import { import { describe, test, expect } from "vitest"; describe("Task", () => { - describe("#execute", async () => { + describe("#perform", async () => { test("fulfilled", async () => { const task = createTask(() => Promise.resolve("Hello World")); expect(task.status).toBe(TaskStatus.Idle); expect(task.isIdle).toBe(true); - task.execute(); + task.perform(); expect(task.status).toBe(TaskStatus.Pending); expect(task.isPending).toBe(true); @@ -34,7 +34,7 @@ describe("Task", () => { expect(task.status).toBe(TaskStatus.Idle); expect(task.isIdle).toBe(true); - task.execute(); + task.perform(); expect(task.status).toBe(TaskStatus.Pending); expect(task.isPending).toBe(true); @@ -54,7 +54,7 @@ describe("Task", () => { expect(task.status).toBe(TaskStatus.Idle); - task.execute(); + task.perform(); expect(task.status).toBe(TaskStatus.Pending);