Skip to content

Commit

Permalink
fix action async execution order when throwing (#246)
Browse files Browse the repository at this point in the history
* fix action async execution order when throwing
  • Loading branch information
xaviergonz committed Apr 7, 2020
1 parent def1e37 commit aad06b1
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# 5.5.7

* Another fix for invalid `actionAsync` context through [#246](https://github.com/mobxjs/mobx-utils/pull/246) by [xaviergonz](https://github.com/xaviergonz)

# 5.5.6

* Another fix for invalid `actionAsync` context when promises resolve at the same time in different actionAsync calls, by [xaviergonz](https://github.com/xaviergonz) through [#244](https://github.com/mobxjs/mobx-utils/pull/244)
* Another fix for invalid `actionAsync` context when promises resolve at the same time in different actionAsync calls through [#244](https://github.com/mobxjs/mobx-utils/pull/244) by [xaviergonz](https://github.com/xaviergonz)

# 5.5.5

Expand Down
4 changes: 4 additions & 0 deletions src/action-async.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ export async function task<R>(value: R | PromiseLike<R>): Promise<R> {
await inOrderExecution()

return ret
} catch (err) {
await inOrderExecution()

throw err
} finally {
// only restart if it not a dangling promise (the action is not yet finished)
if (unfinishedIds.has(runId)) {
Expand Down
42 changes: 42 additions & 0 deletions test/action-async.ts
Original file line number Diff line number Diff line change
Expand Up @@ -578,3 +578,45 @@ test("reusing promises", async () => {
expect(values).toEqual([1, 2, 3])
expectNoActionsRunning()
})

test("actions that throw in parallel", async () => {
mobx.configure({ enforceActions: "observed" })

const r = shouldThrow =>
new Promise((resolve, reject) => {
setTimeout(() => {
if (shouldThrow) {
reject("Error")
return
}
resolve(42)
}, 10)
})

const actionAsync1 = actionAsync("actionAsync1", async () => {
try {
return await task(r(true))
} catch (err) {
return "error"
}
})

const actionAsync2 = actionAsync("actionAsync2", async () => {
try {
return await task(r(false))
} catch (err) {
return "error"
}
})

const result = await Promise.all([actionAsync1(), actionAsync2(), actionAsync1()])

expectNoActionsRunning()
expect(result).toMatchInlineSnapshot(`
Array [
"error",
42,
"error",
]
`)
})

0 comments on commit aad06b1

Please sign in to comment.