SlideShare una empresa de Scribd logo
1 de 14
Orchestrating asynchronous Excel
VBA operations with promises
cPromise primer from Excel Liberation
Excel Liberation for details
Waiting for stuff to finish
 JavaScript is good at doing something else in the
meantime
doSomething ( function (result) {
report(result);
} );
doSomethingElse();
function doSomething (callback) {
var result = process();
callback (result);
}
 VBA prefers waiting for things to finish before doing the
next
result = process()
report (result)
doSomethingElse()
Excel Liberation for details
Orchestration with promises
 JavaScript can quickly become a mess with multiple callbacks. Many
developers are now using Deferred promises for better orchestration. For
example in jQuery
var promise = doSomething();
doSomethingElse();
promise.done ( function (result) {
report(result);
})
.fail ( function (error) {
report (error);
});
function doSomething (callback) {
var d = $.Deferred();
process ( function (result) { d.resolve(result);},
function (error) { d.reject (error);} );
return d.promise();
}
Excel Liberation for details
Orchestration in VBA
 One of the most common is to use withEvents, for example
you can execute ADO asynchronously and handle connection
events by declaring your connection with events
Private WithEvents prConnection As ADODB.Connection
 Other functions allow a callback function name, but put
restrictions on what that can be. For example the XMLHTTP
object has an onReadyStateChange property, but first you
have to figure out how to create a class with a default member.
pResponse.OnReadyStateChange = pAsynch
 Custom classes can declare and raise custom events, but they
are underused and complicated
 VBA needs syntactic gymnastics to deal with callbacks and
often forces the need for a network of global variables
Excel Liberation for details
Promises in VBA
 We’ve seen that VBA has various capabilities. Could
they be used to create a deferred/promise structure
?
 Yes they can!
 Here’s a snippet that will get data from a web site
asynchronously, and populate a range with the result
when its done.
Dim callbacks As New yourCallbacks
loadData("http://www.mcpher.com", Range("rest!a1"))
.done(callbacks, "populate")
.fail callbacks, "show“
doSomethingElseInTheMeantime()
Excel Liberation for details
Difference between cPromise and cDeferred
A new cDeferred is created by some task that will complete later. Each cDeferred has a
single cPromise
3 simple rules.
 Deferred is used by the function doing the work
 Promise is used by the function receiving the results.
 Function doing the work returns the promise method of the deferred
Function doing the work
return deferred.promise()
And later
deferred.reject( array(.. Some arguments..))
deferred.resolve( array(..some arguments..))
Function receiving the results
promise.fail (callbacks, “handleit”)
promise.done(callbacks,”processit”)
Excel Liberation for details
Error handling
An issue with asynch and event processing in VBA is how
to communicate errors to the caller. Using promises its
easy. A promise either fails or succeeds. On completion,
if it fails the .fail() method is executed, otherwise the
.done() method is executed.
And it used like this, where you’ve written a handler in your
callbacks class
promise.fail (callbacks, “handleit”)
The arguments you registered at the time of rejection will
be passed to your handler
deferred.reject (Array(“it failed”, statusCode,
someOtherInfo))
Excel Liberation for details
Success handling
Success handling and error handling techniques turn
out to be the same when using promises.
You’ve written a handler in your callbacks class
promise.done (callbacks, “processIt”)
The arguments you registered at the time of resolution
will be passed to your handler
deferred.resolve (Array(“it worked”,data))
Excel Liberation for details
Setup – cDeferred and cPromise classes
 Here’s the classes that are provided
 cDeferred – like $.Deferred() – is used to .resolve() or
.reject() a promise, and to provide the promise() instance
 cPromise – the .done() and .fail() methods set up what to
do on resolution or rejection, very much like the jQuery
.done() and .fail() methods. I dont provide .then() or
$.Where() but may add them later
Excel Liberation for details
Setup – creating a function that returns a
promise
 Every one of these will be different since VBA has
multiple ways of dealing with asynchronicity.
However they must follow this structure
Set d = new cDeferred (create an instance)
.. Do something asyncronous (passing a reference to d)
return d.promise()
Excel Liberation for details
Setup – doing something asynchronous
 The asynchronous function must
Signal completion using the deferred instance that returned
the promise in the caller function like this
d.resolve(..arguments...)
Or
d.reject (..arguments)
Excel Liberation for details
Setup – arguments
 The asynchronous function should also return some
arguments. These will be passed on to the function
that is eventually called on completion of the task
Arguments can be of any type and number, but should be
wrapped in an array like this. This protects them from
being incorrectly handled when passing through the
chain.
d.resolve (Array(.url, .response.responseText, .optionalArgument))
Excel Liberation for details
Memory and scope
One of the problems with asynch and event processing in VBA is that you can end up with many global or module level
variables to communicate. Using promises to pass arguments avoids this need, since the promise resolution
records a reference to the results data to be passed, and thus prevents it going out of scope.
When you create an proc that is going to behave asynchronously though, there is a chance that you will find local
variables going out of scope and therefore events not firing – this is nothing to do with promises – but a regular
issue with orchestration in VBA. One solution is to use module level variables. Another is to make reference to a
transient variable in a single, module level object.
I provide a register class to register asynch variables with, which is declared at module level
Private register As cDeferredRegister
And used like this
Dim ca As cHttpDeferred
Set ca = New cHttpDeferred
register.register ca
You should create a teardown method in any classes you create that have special memory leak prevention needs.
Register.teardown will clean itself up and execute any teardowns with any object instances that have been
registered
register.tearDown()
Excel Liberation for details
Summary
These examples start to address how asynchronicity
might be better orchestrated in VBA within the
limitations of the available syntax. Over time I will
add other promise related functions such as when()
Memory leaks relating to asynchronicity are not
resolved by this, but can at least be identified and
mitigated by this cleaner orchestration.
For more detail, examples, and to download see Excel
Liberation

Más contenido relacionado

Más de Bruce McPherson

Do something in 5 with gas 4- Get your analytics profiles to a spreadsheet
Do something in 5 with gas 4- Get your analytics profiles to a spreadsheetDo something in 5 with gas 4- Get your analytics profiles to a spreadsheet
Do something in 5 with gas 4- Get your analytics profiles to a spreadsheetBruce McPherson
 
Do something in 5 with gas 3-simple invoicing app
Do something in 5 with gas 3-simple invoicing appDo something in 5 with gas 3-simple invoicing app
Do something in 5 with gas 3-simple invoicing appBruce McPherson
 
Do something in 5 with gas 2-graduate to a database
Do something in 5 with gas 2-graduate to a databaseDo something in 5 with gas 2-graduate to a database
Do something in 5 with gas 2-graduate to a databaseBruce McPherson
 
Do something in 5 minutes with gas 1-use spreadsheet as database
Do something in 5 minutes with gas 1-use spreadsheet as databaseDo something in 5 minutes with gas 1-use spreadsheet as database
Do something in 5 minutes with gas 1-use spreadsheet as databaseBruce McPherson
 
Google apps script database abstraction exposed version
Google apps script database abstraction   exposed versionGoogle apps script database abstraction   exposed version
Google apps script database abstraction exposed versionBruce McPherson
 
Google cloud datastore driver for Google Apps Script DB abstraction
Google cloud datastore driver for Google Apps Script DB abstractionGoogle cloud datastore driver for Google Apps Script DB abstraction
Google cloud datastore driver for Google Apps Script DB abstractionBruce McPherson
 
JavaScript client API for Google Apps Script API primer
JavaScript client API for Google Apps Script API primerJavaScript client API for Google Apps Script API primer
JavaScript client API for Google Apps Script API primerBruce McPherson
 
VBA API for scriptDB primer
VBA API for scriptDB primerVBA API for scriptDB primer
VBA API for scriptDB primerBruce McPherson
 
Javascript like objects and JSON processing in VBA
Javascript like objects and JSON processing in VBAJavascript like objects and JSON processing in VBA
Javascript like objects and JSON processing in VBABruce McPherson
 

Más de Bruce McPherson (10)

Do something in 5 with gas 4- Get your analytics profiles to a spreadsheet
Do something in 5 with gas 4- Get your analytics profiles to a spreadsheetDo something in 5 with gas 4- Get your analytics profiles to a spreadsheet
Do something in 5 with gas 4- Get your analytics profiles to a spreadsheet
 
Do something in 5 with gas 3-simple invoicing app
Do something in 5 with gas 3-simple invoicing appDo something in 5 with gas 3-simple invoicing app
Do something in 5 with gas 3-simple invoicing app
 
Do something in 5 with gas 2-graduate to a database
Do something in 5 with gas 2-graduate to a databaseDo something in 5 with gas 2-graduate to a database
Do something in 5 with gas 2-graduate to a database
 
Do something in 5 minutes with gas 1-use spreadsheet as database
Do something in 5 minutes with gas 1-use spreadsheet as databaseDo something in 5 minutes with gas 1-use spreadsheet as database
Do something in 5 minutes with gas 1-use spreadsheet as database
 
Google apps script database abstraction exposed version
Google apps script database abstraction   exposed versionGoogle apps script database abstraction   exposed version
Google apps script database abstraction exposed version
 
Google cloud datastore driver for Google Apps Script DB abstraction
Google cloud datastore driver for Google Apps Script DB abstractionGoogle cloud datastore driver for Google Apps Script DB abstraction
Google cloud datastore driver for Google Apps Script DB abstraction
 
Dbabstraction
DbabstractionDbabstraction
Dbabstraction
 
JavaScript client API for Google Apps Script API primer
JavaScript client API for Google Apps Script API primerJavaScript client API for Google Apps Script API primer
JavaScript client API for Google Apps Script API primer
 
VBA API for scriptDB primer
VBA API for scriptDB primerVBA API for scriptDB primer
VBA API for scriptDB primer
 
Javascript like objects and JSON processing in VBA
Javascript like objects and JSON processing in VBAJavascript like objects and JSON processing in VBA
Javascript like objects and JSON processing in VBA
 

Último

Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilV3cube
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024The Digital Insurer
 

Último (20)

Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 

Orchestrating asynchronicity in Excel VBA using promises

  • 1. Orchestrating asynchronous Excel VBA operations with promises cPromise primer from Excel Liberation
  • 2. Excel Liberation for details Waiting for stuff to finish  JavaScript is good at doing something else in the meantime doSomething ( function (result) { report(result); } ); doSomethingElse(); function doSomething (callback) { var result = process(); callback (result); }  VBA prefers waiting for things to finish before doing the next result = process() report (result) doSomethingElse()
  • 3. Excel Liberation for details Orchestration with promises  JavaScript can quickly become a mess with multiple callbacks. Many developers are now using Deferred promises for better orchestration. For example in jQuery var promise = doSomething(); doSomethingElse(); promise.done ( function (result) { report(result); }) .fail ( function (error) { report (error); }); function doSomething (callback) { var d = $.Deferred(); process ( function (result) { d.resolve(result);}, function (error) { d.reject (error);} ); return d.promise(); }
  • 4. Excel Liberation for details Orchestration in VBA  One of the most common is to use withEvents, for example you can execute ADO asynchronously and handle connection events by declaring your connection with events Private WithEvents prConnection As ADODB.Connection  Other functions allow a callback function name, but put restrictions on what that can be. For example the XMLHTTP object has an onReadyStateChange property, but first you have to figure out how to create a class with a default member. pResponse.OnReadyStateChange = pAsynch  Custom classes can declare and raise custom events, but they are underused and complicated  VBA needs syntactic gymnastics to deal with callbacks and often forces the need for a network of global variables
  • 5. Excel Liberation for details Promises in VBA  We’ve seen that VBA has various capabilities. Could they be used to create a deferred/promise structure ?  Yes they can!  Here’s a snippet that will get data from a web site asynchronously, and populate a range with the result when its done. Dim callbacks As New yourCallbacks loadData("http://www.mcpher.com", Range("rest!a1")) .done(callbacks, "populate") .fail callbacks, "show“ doSomethingElseInTheMeantime()
  • 6. Excel Liberation for details Difference between cPromise and cDeferred A new cDeferred is created by some task that will complete later. Each cDeferred has a single cPromise 3 simple rules.  Deferred is used by the function doing the work  Promise is used by the function receiving the results.  Function doing the work returns the promise method of the deferred Function doing the work return deferred.promise() And later deferred.reject( array(.. Some arguments..)) deferred.resolve( array(..some arguments..)) Function receiving the results promise.fail (callbacks, “handleit”) promise.done(callbacks,”processit”)
  • 7. Excel Liberation for details Error handling An issue with asynch and event processing in VBA is how to communicate errors to the caller. Using promises its easy. A promise either fails or succeeds. On completion, if it fails the .fail() method is executed, otherwise the .done() method is executed. And it used like this, where you’ve written a handler in your callbacks class promise.fail (callbacks, “handleit”) The arguments you registered at the time of rejection will be passed to your handler deferred.reject (Array(“it failed”, statusCode, someOtherInfo))
  • 8. Excel Liberation for details Success handling Success handling and error handling techniques turn out to be the same when using promises. You’ve written a handler in your callbacks class promise.done (callbacks, “processIt”) The arguments you registered at the time of resolution will be passed to your handler deferred.resolve (Array(“it worked”,data))
  • 9. Excel Liberation for details Setup – cDeferred and cPromise classes  Here’s the classes that are provided  cDeferred – like $.Deferred() – is used to .resolve() or .reject() a promise, and to provide the promise() instance  cPromise – the .done() and .fail() methods set up what to do on resolution or rejection, very much like the jQuery .done() and .fail() methods. I dont provide .then() or $.Where() but may add them later
  • 10. Excel Liberation for details Setup – creating a function that returns a promise  Every one of these will be different since VBA has multiple ways of dealing with asynchronicity. However they must follow this structure Set d = new cDeferred (create an instance) .. Do something asyncronous (passing a reference to d) return d.promise()
  • 11. Excel Liberation for details Setup – doing something asynchronous  The asynchronous function must Signal completion using the deferred instance that returned the promise in the caller function like this d.resolve(..arguments...) Or d.reject (..arguments)
  • 12. Excel Liberation for details Setup – arguments  The asynchronous function should also return some arguments. These will be passed on to the function that is eventually called on completion of the task Arguments can be of any type and number, but should be wrapped in an array like this. This protects them from being incorrectly handled when passing through the chain. d.resolve (Array(.url, .response.responseText, .optionalArgument))
  • 13. Excel Liberation for details Memory and scope One of the problems with asynch and event processing in VBA is that you can end up with many global or module level variables to communicate. Using promises to pass arguments avoids this need, since the promise resolution records a reference to the results data to be passed, and thus prevents it going out of scope. When you create an proc that is going to behave asynchronously though, there is a chance that you will find local variables going out of scope and therefore events not firing – this is nothing to do with promises – but a regular issue with orchestration in VBA. One solution is to use module level variables. Another is to make reference to a transient variable in a single, module level object. I provide a register class to register asynch variables with, which is declared at module level Private register As cDeferredRegister And used like this Dim ca As cHttpDeferred Set ca = New cHttpDeferred register.register ca You should create a teardown method in any classes you create that have special memory leak prevention needs. Register.teardown will clean itself up and execute any teardowns with any object instances that have been registered register.tearDown()
  • 14. Excel Liberation for details Summary These examples start to address how asynchronicity might be better orchestrated in VBA within the limitations of the available syntax. Over time I will add other promise related functions such as when() Memory leaks relating to asynchronicity are not resolved by this, but can at least be identified and mitigated by this cleaner orchestration. For more detail, examples, and to download see Excel Liberation