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
.async2() are already provided and they require a callback and you aren't allowed to modify them, then you can't achieve
Item.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 no 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 originally 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. They do that by doing the following:
So, if the original async methods that expect callbacks were
.async2(), you could create
.async2Chain() such that you could make it work like this:
.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.