Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unwind recursion in loopAsync when possible #2923

Merged
merged 3 commits into from
Jan 18, 2016
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Unwind the done callback as well
  • Loading branch information
taion committed Jan 18, 2016
commit 6c441bec47dab73a1968dc43279d1059eb9c7a34
19 changes: 16 additions & 3 deletions modules/AsyncUtils.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
export function loopAsync(turns, work, callback) {
let currentTurn = 0, isDone = false
let sync = false, hasNext = false
let sync = false, hasNext = false, doneArgs

function done() {
isDone = true
if (sync) {
// Iterate instead of recursing if possible.
doneArgs = [ ...arguments ]
return
}

callback.apply(this, arguments)
}

function next() {
hasNext = true
if (isDone) {
return
}

hasNext = true
if (sync) {
// Iterate instead of recursing if possible.
return
@@ -25,10 +34,14 @@ export function loopAsync(turns, work, callback) {
sync = false

if (isDone) {
// This means the loop finished synchronously.
callback.apply(this, doneArgs)
return
}

if (currentTurn >= turns && hasNext) {
done()
isDone = true
callback()
}
}