SlideShare une entreprise Scribd logo
1  sur  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

Contenu connexe

Plus 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
 

Plus 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
 

Dernier

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
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
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
 
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
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
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
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
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
 
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
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 

Dernier (20)

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
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
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
 
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
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
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...
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
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...
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 

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