Skip to content

Commit 4b4fe1b

Browse files
committed
feat: add Promise.prototype.toTask
1 parent 7a1a7ae commit 4b4fe1b

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

src/Task/Task.ts

+10
Original file line numberDiff line numberDiff line change
@@ -989,3 +989,13 @@ export class ExternalTask<E, S> extends Task<E, S> {
989989
}
990990
}
991991
}
992+
993+
declare global {
994+
interface Promise<T> {
995+
toTask(): Task<unknown, T>
996+
}
997+
}
998+
999+
Promise.prototype.toTask = function <S>(this: Promise<S>): Task<unknown, S> {
1000+
return fromPromise(this)
1001+
}

src/Task/__tests__/fromPromise.spec.ts

+14
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,18 @@ describe("fromPromise", () => {
5757
expect(reject).toBeCalledWith(ERROR_RESULT)
5858
expect(resolve).not.toBeCalled()
5959
})
60+
61+
test("should succeed using prototype helper", async () => {
62+
const resolve = jest.fn()
63+
const reject = jest.fn()
64+
65+
const promise = Promise.resolve(SUCCESS_RESULT)
66+
67+
promise.toTask().fork(reject, resolve)
68+
69+
await promise.catch(() => void 0)
70+
71+
expect(resolve).toBeCalledWith(SUCCESS_RESULT)
72+
expect(reject).not.toBeCalled()
73+
})
6074
})

0 commit comments

Comments
 (0)