Hacker News new | ask | show | jobs
by abecedarius 4824 days ago
A couple suggestions for this code:

- Couldn't it be simpler without the linked-list queue? Use an array: the first ndispatched are for calls that have been invoked; they each hold either the corresponding result or undefined if not yet resolved by their callback. The rest of the array holds arguments objects for pending calls. When it's time to notify, this array is the results array.

- The 2-space indent plus fairly deep nesting makes it hard to see where the 'main story' starts and ends, at least for me. 4-space indent goes better with this nested style.

1 comments

Using the results array to store the scheduled tasks is an interesting idea. Thanks for the suggestion!
You're welcome! I tried coding this up at https://gist.github.com/darius/5287542 (untested). I see you have your own version now, which is longer but has a loop where I fell back to the tail call. (I also cut out a line in notify(), though you might prefer the old behavior of passing undefined for the results array on error, vs. the new one of an empty array. I guess I would prefer the old behavior on reflection.)

Some doc suggestions from my draft and I'll finally shut up:

    //  Can you call things after the first .await or .awaitAll?
    //  Whatever the answer, it should be documented.
    //  Maybe document the assumption that callbacks get invoked exactly once?
    //  (It'd be possible to ensure it's at most once.)
    //  Document assumption: the notified awaitAll function won't mess with its array argument
    //  (this matters if we're called again after).