What's the best way to write a series of deeply nested callbacks?
You have several functions, each of which performs an asynchronous operation and which depends on the prior function's output. For two functions, this typically looks like foo(bar)
.
A long series of callbacks will look like foo(bar(baz(qux(...))))
. This quickly becomes very difficult to read, let alone maintain. Especially once you begin passing additional parameters to the function calls! Step improves on this a bit, but adds an extra parameter to each function call.
This post (section 3.1) suggests a wait function, which is used like this: wait(foo, qux)
and supports multiple dependencies like so: wait([foo, bar, baz], qux)
. Unfortunately, this won't help for nested callbacks.
Step and wait both help a bit, but neither seems ideal. Is there a better way to write long callback chains?