promise framework is implemented. This is kind of extreme VBA and Excel is very unforgiving. If you are playing around with this, I advise saving often.Here's how the
These two classes tend to be used interchangeably in some of my descriptions, but they are quite different. In my VBA implementation, a promise is a property of a deferred. A deferred is used to signal status and record the data to be passed following some action, whereas a promise is the messenger of that status. Generally speaking deferreds are accessed by the performer of the action just once, and are about sending a signal. Promises are used to react to the resolution of an action and are about receiving a signal.
A typical app might look like this in pseudo code, where the 'process' class would take care of doing something with data retrieved asynchronously.
The deferred object is the parent of the cPromise class. The main interface members of the cDeferred class are
Each of these signal their .promise by raising a .completed( array(data) ) event, which .promise is listening for.
The cPromise object is the child of the cDeferred class. A completed event in its parent cDeferred class will provoke an execute of either the queue of actions in the .done() queue or in the .fail() queue. The main interface members of the cPromise class are
The cWhen class is not normally called directly, but rather through the when() function. The interface to when is
All promises is in all arrays need to be resolved or rejected for the promise that is returned by when to get either resolved or rejected. If any promise fails, the promise returned will be rejected. All promises need to be resolved successfully for the when.promise to be resolved. As a convenience, the data associated with the last promise in all arrays is passed to the .done() or .fail() method of when.promise
when returns a cPromise, which means that you can chain .done() or .fail() to a when() function.
In order to avoid dropping out of memory, out of scope variable are referenced in a public register. This class is normally accessed through these functions.
This adds an object reference with the given name and type. The name and type are only used for later listing and debugging. Self reference is returned for chaining.
Removes all register items and attempts to execute a tearDown class if they have one. Should be called when all aync activity is ended, and as very first call in a fresh session.
Can be useful for debugging. It will produce a list like this.
register.listthese items are being kept in memory248811352 cProcessData266750400 cEventTimer266749920 cBackoff264238376 cHttpDeferredHelper358537424 cHttpDeferredschema cJobject266750592 cEventTimer266750496 cBackoff264239816 cHttpDeferredHelper269075400 cHttpDeferred
One of the trickiest things is to get the arguments right for reject,resolve,done and fail. If you get that wrong, the error will show here in the cPromise() class
CallByName cb.callback, cb.method, VbMethod, getData, cb.defer, cb.args
To identify which object is being called, inspect ObjPtr(cb.callback) which will be one of the addresses returned by register.list , and cb.Method which will be a string with the name of the method within the class.
Since this is in the early stages of development the code is in a separate workbook (promises.xlsm) and can be downloaded here
Services > Desktop Liberation - the definitive resource for Google Apps Script and Microsoft Office automation > Classes > Promises in VBA >