SlideShare uma empresa 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

Mais conteúdo relacionado

Mais 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
 

Mais 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

Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfOverkill Security
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
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
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...apidays
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusZilliz
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Angeliki Cooney
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...apidays
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...apidays
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodJuan lago vázquez
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native ApplicationsWSO2
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyKhushali Kathiriya
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...DianaGray10
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businesspanagenda
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamUiPathCommunity
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxRustici Software
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Victor Rentea
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsNanddeep Nachan
 

Último (20)

Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
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
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 

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