Skip to content

Commit

Permalink
feat: add Promise.prototype.toTask
Browse files Browse the repository at this point in the history
  • Loading branch information
tdreyno committed Jan 8, 2021
1 parent 7a1a7ae commit 4b4fe1b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/Task/Task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -989,3 +989,13 @@ export class ExternalTask<E, S> extends Task<E, S> {
}
}
}

declare global {
interface Promise<T> {
toTask(): Task<unknown, T>
}
}

Promise.prototype.toTask = function <S>(this: Promise<S>): Task<unknown, S> {
return fromPromise(this)
}
14 changes: 14 additions & 0 deletions src/Task/__tests__/fromPromise.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})
})

0 comments on commit 4b4fe1b

Please sign in to comment.