I'm trying to add a "default callback" to a prototype that will assign a callback function (in the form of a promise) to an async method if none is provided.
The goal is to have a class's chain of async methods run synchronously
Item.async1().async2()....asyncN()
Mind you, the async functions themselves expect a callback but they're not passed as arguments in the function call (which tells me that the class needs a default behavior when the callback lookup fails)
Spec states that I cannot directly modify the behavior or side effects of the prototype methods. I can add prototype methods. We have no visibility into how these prototype methods are implemented.
TLDR: Without modifying the prototype methods, how can you chain an N number of async methods and ensure they run sequentially?
BTW: Promisifying the prototype methods in question would be helpful if I wanted to implement the promisified versions, but it looks like we're constrained to the original function calls
I whould suggest you to use a library for that, i made one myself that allow not only secuential chaining but let you use loops and ifElse structures.
https://github.com/Raising/PromiseChain
(note that we are not using a parent object in this example, that cleans the code a lot) The internal scope save the result of each continue with the name provided
Alternatively if all functions belong to the same object and only need the scope as parameter you can do this
Well, I wasn't going to answer - but I was challenged.
It's quite easy to utilize the built in abilities of promises in order to get this sort of queuing for free. Here is how this conversion will work:
then
, so they'll queue asthen
is the queueing mechanism promises have.Note: The
promisify
andpromisifyAll
methods I write here - you should grab off NPM - lots of good and fast usages that take a promise constructor.First, we need a method that converts a callback API to promises:
Now, let's promisify the whole object:
So far, nothing new, lots of NPM libraries do this - now for the magic of promises - let's create a method that executes functions on an original object in a
then
:Now, let's wrap things up
And that's it, if we want the example to be self contained (again, promise and promisifyAll are provided by a library usually):
Or with a library that does promisify:
And what's an answer without a demo:
Copy paste this to your friendly neighborhood console and see for yourself :)
To prove this preserves state on the original object (it's easy to modify it not to if that's a requirement) let's add an
i
variable and increment it in delay and see that things work:If
.async1()
and.async2()
are already provided and they require a callback and you aren't allowed to modify them, then you can't achieveItem.async1().async2()....asyncN()
. The methods you're calling just aren't built to work that way and if you're not allowed to change them, there's not much you can do other than replace those methods with methods that do work the way you want.If you can create new methods with their own names that internally use the original methods, then that can be done. One model for how to do that is jQuery animations. They allow you do things like this:
And each of those async operations will be chained together. jQuery accomplishes that by doing the following:
So, if the original async methods that expect callbacks were
.async1()
and.async2()
, you could create.async1Chain()
and.async2Chain()
such that you could make it work like this:Where internally,
.async1Chain()
would call.async1()
with a local callback and that callback would be configured to check the queue to run the next queued operation if there was one.This is just one method of solving the problem. There are likely others.