It's difficult to mentally follow the chain of promises here, because you have an async function (const asyncLimit = async...) which returns an async function (return async function) which itself awaits at least two promises (one in a loop) before resolving.
Note that asyncLimit does not have to be async, as it does not use await; removing async from the function signature would help comprehension a bit, since you would reduce one level of Promise-ception and stay out of limbo.
I guess it doesn't really matter, because the promise returned by asyncLimit can be used by the calling code. But attaching a finally clause to p instead of awaiting it allows the function to return immediately, instead of waiting for p to resolve.
Hey! I'm YCMJason, a Software Engineer in London 👨💻. Love diving into tech puzzles and sharing them! 🧩
All views expressed here are my own opinions, so please take them with a pinch of salt! 🧂
It's difficult to mentally follow the chain of promises here, because you have an async function (
const asyncLimit = async...
) which returns an async function (return async function
) which itself awaits at least two promises (one in a loop) before resolving.Note that
asyncLimit
does not have to be async, as it does not useawait
; removingasync
from the function signature would help comprehension a bit, since you would reduce one level of Promise-ception and stay out of limbo.I guess it doesn't really matter, because the promise returned by
asyncLimit
can be used by the calling code. But attaching afinally
clause top
instead ofawait
ing it allows the function to return immediately, instead of waiting forp
to resolve.oh, I mistyped haha! Thanks for catching this!
asyncLimit
shouldn't be an async function.