Skip to content

Commit

Permalink
feat: add Task.wrapPromiseCreator
Browse files Browse the repository at this point in the history
  • Loading branch information
tdreyno committed Jan 8, 2021
1 parent 10e1f85 commit a8cc72a
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
25 changes: 25 additions & 0 deletions docs/task-static.md
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,31 @@ type fromLazyPromise = <S, E = any>(
{% endtab %}
{% endtabs %}
## wrapPromiseCreator
Given a function which returns a Promise, create a new function which given the same arguments will now return a Task instead.
{% tabs %}
{% tab title="Usage" %}
```typescript
const taskFetch = wrapPromiseCreator(fetch)
const task: Task<never, Response> = taskFetch(URL)
```

{% endtab %}

{% tab title="Type Definition" %}

```typescript
type wrapPromiseCreator = <S, Args extends unknown[]>(
fn: (...args: Args) => Promise<S>,
) => (...args: Args): Task<unknown, S>
```
{% endtab %}
{% endtabs %}
## race
Creates a task that will always run an array of tasks in **parallel**. The first task to finish is the resulting error or value. Useful for implementing network request timeouts by racing a task which fails in x milliseconds and a task which makes the request.
Expand Down
10 changes: 10 additions & 0 deletions src/Task/Task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,15 @@ export const fromLazyPromise = <S>(
getPromise: () => S | Promise<S>,
): Task<unknown, S> => succeedBy(getPromise).chain(fromPromise)

/**
* Given a function that returns a promise, return a new function that
* lazily returns a Task instead.
* @param fn A function which returns a promise
*/
export const wrapPromiseCreator = <S, Args extends unknown[]>(
fn: (...args: Args) => Promise<S>,
) => (...args: Args): Task<unknown, S> => fromLazyPromise(() => fn(...args))

/**
* Given a task, create a Promise which resolves when the task does.
* @param task The task we will convert to a promise.
Expand Down Expand Up @@ -700,6 +709,7 @@ export class Task<E, S> implements PromiseLike<S> {
public static fromPromise = fromPromise
public static fromPromises = fromPromises
public static fromLazyPromise = fromLazyPromise
public static wrapPromiseCreator = wrapPromiseCreator
public static race = race
public static external = external
public static emitter = emitter
Expand Down
37 changes: 37 additions & 0 deletions src/Task/__tests__/wrapPromiseCreator.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/* eslint-disable @typescript-eslint/no-unused-vars */
import { wrapPromiseCreator } from "../Task"
import { ERROR_RESULT, SUCCESS_RESULT } from "./util"

describe("wrapPromiseCreator", () => {
test("should succeed when a promise succeeds", async () => {
const resolve = jest.fn()
const reject = jest.fn()

const promise = Promise.resolve(SUCCESS_RESULT)
const makePromise = (_a: string, _b: number) => promise

const runTask = wrapPromiseCreator(makePromise)
runTask("test", 1).fork(reject, resolve)

await promise

expect(resolve).toBeCalledWith(SUCCESS_RESULT)
expect(reject).not.toBeCalled()
})

test("should fail when a promise fails", async () => {
const resolve = jest.fn()
const reject = jest.fn()

const promise = Promise.reject(ERROR_RESULT)
const makePromise = (_a: number, _b: string) => promise

const runTask = wrapPromiseCreator(makePromise)
runTask(1, "test").fork(reject, resolve)

await promise.catch(() => void 0)

expect(reject).toBeCalledWith(ERROR_RESULT)
expect(resolve).not.toBeCalled()
})
})

0 comments on commit a8cc72a

Please sign in to comment.