From 4b4fe1bf3ceffee013788f1ad62b29b8fe1d4300 Mon Sep 17 00:00:00 2001 From: Thomas Reynolds Date: Fri, 8 Jan 2021 04:53:36 +0000 Subject: [PATCH] feat: add Promise.prototype.toTask --- src/Task/Task.ts | 10 ++++++++++ src/Task/__tests__/fromPromise.spec.ts | 14 ++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/src/Task/Task.ts b/src/Task/Task.ts index ee2e58c7..17d6e668 100644 --- a/src/Task/Task.ts +++ b/src/Task/Task.ts @@ -989,3 +989,13 @@ export class ExternalTask extends Task { } } } + +declare global { + interface Promise { + toTask(): Task + } +} + +Promise.prototype.toTask = function (this: Promise): Task { + return fromPromise(this) +} diff --git a/src/Task/__tests__/fromPromise.spec.ts b/src/Task/__tests__/fromPromise.spec.ts index 689e5d41..ce4c6f2f 100644 --- a/src/Task/__tests__/fromPromise.spec.ts +++ b/src/Task/__tests__/fromPromise.spec.ts @@ -57,4 +57,18 @@ describe("fromPromise", () => { expect(reject).toBeCalledWith(ERROR_RESULT) expect(resolve).not.toBeCalled() }) + + test("should succeed using prototype helper", async () => { + const resolve = jest.fn() + const reject = jest.fn() + + const promise = Promise.resolve(SUCCESS_RESULT) + + promise.toTask().fork(reject, resolve) + + await promise.catch(() => void 0) + + expect(resolve).toBeCalledWith(SUCCESS_RESULT) + expect(reject).not.toBeCalled() + }) })