Skip to content

Commit

Permalink
Merge ab46fcc into 4927162
Browse files Browse the repository at this point in the history
  • Loading branch information
xaviergonz committed Oct 15, 2019
2 parents 4927162 + ab46fcc commit 32a74a6
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 9 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
@@ -1,3 +1,5 @@
* `task` now supports plain values as well.

# 5.5.0

_Note: the minimum required MobX version for this release has been bumped to `"mobx": "^4.13.1 || ^5.13.1"`_
Expand Down
9 changes: 2 additions & 7 deletions src/action-async.ts
Expand Up @@ -27,12 +27,7 @@ function getCurrentActionAsyncContext() {
return actionAsyncContextStack[actionAsyncContextStack.length - 1]!
}

export async function task<R>(promise: Promise<R>): Promise<R> {
invariant(
typeof promise === "object" && typeof promise.then === "function",
"'task' expects a promise"
)

export async function task<R>(value: R | PromiseLike<R>): Promise<R> {
const ctx = getCurrentActionAsyncContext()

const { runId, actionName, args, scope, actionRunInfo, step } = ctx
Expand All @@ -42,7 +37,7 @@ export async function task<R>(promise: Promise<R>): Promise<R> {
currentlyActiveIds.delete(runId)

try {
return await promise
return await value
} finally {
// only restart if it not a dangling promise (the action is not yet finished)
if (unfinishedIds.has(runId)) {
Expand Down
5 changes: 3 additions & 2 deletions test/action-async.ts
Expand Up @@ -46,12 +46,13 @@ test("it should support async actions", async () => {
x.a = await task(delay(100, 3))
await task(delay(100, 0))
x.a = 4
x.a = await task(5)
return x.a
})

const v = await f(2)
expect(v).toBe(4)
expect(values).toEqual([1, 2, 3, 4])
expect(v).toBe(5)
expect(values).toEqual([1, 2, 3, 4, 5])
expectNoActionsRunning()
})

Expand Down

0 comments on commit 32a74a6

Please sign in to comment.