Skip to content

Commit

Permalink
Merge a58d385 into 6454b14
Browse files Browse the repository at this point in the history
  • Loading branch information
xaviergonz committed Apr 4, 2020
2 parents 6454b14 + a58d385 commit 6b367f2
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
@@ -1,3 +1,5 @@
* 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)

# 5.5.5

* Fixed tree-shaking mobx-utils, see [#238](https://github.com/mobxjs/mobx-utils/pull/238) by [IgorBabkin](https://github.com/IgorBabkin)
Expand Down
11 changes: 7 additions & 4 deletions src/action-async.ts
Expand Up @@ -16,8 +16,11 @@ interface IActionAsyncContext {
args: IArguments
}

let taskOrderPromise = Promise.resolve()
const emptyFunction = () => Promise.resolve()
let taskOrderPromise: Promise<any> = Promise.resolve()

// we use this trick to force a proper order of execution even for immediately resolved promises
// for older versions of node (< 10)
const emptyFunction = () => Promise.all([Promise.resolve()])

const actionAsyncContextStack: IActionAsyncContext[] = []

Expand All @@ -41,9 +44,9 @@ export async function task<R>(value: R | PromiseLike<R>): Promise<R> {

// we use this trick to force a proper order of execution
// even for immediately resolved promises
// we need to also use catch or else it won't work for older versions of node (< 10)
// we need to also use then twice or else it won't work for older versions of node (< 10)
// since it would resolve them immediately
taskOrderPromise = taskOrderPromise.then(emptyFunction).catch(emptyFunction)
taskOrderPromise = taskOrderPromise.then(emptyFunction).then(emptyFunction)
await taskOrderPromise

return ret
Expand Down

0 comments on commit 6b367f2

Please sign in to comment.