SlideShare a Scribd company logo
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

More Related Content

More from 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 spreadsheet
Bruce 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 app
Bruce 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 database
Bruce 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 database
Bruce 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 version
Bruce 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 abstraction
Bruce McPherson
 
Dbabstraction
DbabstractionDbabstraction
Dbabstraction
Bruce 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 primer
Bruce McPherson
 
VBA API for scriptDB primer
VBA API for scriptDB primerVBA API for scriptDB primer
VBA API for scriptDB primer
Bruce 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 VBA
Bruce McPherson
 

More from 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
 

Recently uploaded

find out more about the role of autonomous vehicles in facing global challenges
find out more about the role of autonomous vehicles in facing global challengesfind out more about the role of autonomous vehicles in facing global challenges
find out more about the role of autonomous vehicles in facing global challenges
huseindihon
 
Using LLM Agents with Llama 3, LangGraph and Milvus
Using LLM Agents with Llama 3, LangGraph and MilvusUsing LLM Agents with Llama 3, LangGraph and Milvus
Using LLM Agents with Llama 3, LangGraph and Milvus
Zilliz
 
BT & Neo4j: Knowledge Graphs for Critical Enterprise Systems.pptx.pdf
BT & Neo4j: Knowledge Graphs for Critical Enterprise Systems.pptx.pdfBT & Neo4j: Knowledge Graphs for Critical Enterprise Systems.pptx.pdf
BT & Neo4j: Knowledge Graphs for Critical Enterprise Systems.pptx.pdf
Neo4j
 
“Deploying Large Language Models on a Raspberry Pi,” a Presentation from Usef...
“Deploying Large Language Models on a Raspberry Pi,” a Presentation from Usef...“Deploying Large Language Models on a Raspberry Pi,” a Presentation from Usef...
“Deploying Large Language Models on a Raspberry Pi,” a Presentation from Usef...
Edge AI and Vision Alliance
 
Amul milk launches in US: Key details of its new products ...
Amul milk launches in US: Key details of its new products ...Amul milk launches in US: Key details of its new products ...
Amul milk launches in US: Key details of its new products ...
chetankumar9855
 
High Profile Girls Call ServiCe Hyderabad 0000000000 Tanisha Best High Class ...
High Profile Girls Call ServiCe Hyderabad 0000000000 Tanisha Best High Class ...High Profile Girls Call ServiCe Hyderabad 0000000000 Tanisha Best High Class ...
High Profile Girls Call ServiCe Hyderabad 0000000000 Tanisha Best High Class ...
aslasdfmkhan4750
 
Choose our Linux Web Hosting for a seamless and successful online presence
Choose our Linux Web Hosting for a seamless and successful online presenceChoose our Linux Web Hosting for a seamless and successful online presence
Choose our Linux Web Hosting for a seamless and successful online presence
rajancomputerfbd
 
Recent Advancements in the NIST-JARVIS Infrastructure
Recent Advancements in the NIST-JARVIS InfrastructureRecent Advancements in the NIST-JARVIS Infrastructure
Recent Advancements in the NIST-JARVIS Infrastructure
KAMAL CHOUDHARY
 
Dublin_mulesoft_meetup_Mulesoft_Salesforce_Integration (1).pptx
Dublin_mulesoft_meetup_Mulesoft_Salesforce_Integration (1).pptxDublin_mulesoft_meetup_Mulesoft_Salesforce_Integration (1).pptx
Dublin_mulesoft_meetup_Mulesoft_Salesforce_Integration (1).pptx
Kunal Gupta
 
WPRiders Company Presentation Slide Deck
WPRiders Company Presentation Slide DeckWPRiders Company Presentation Slide Deck
WPRiders Company Presentation Slide Deck
Lidia A.
 
Comparison Table of DiskWarrior Alternatives.pdf
Comparison Table of DiskWarrior Alternatives.pdfComparison Table of DiskWarrior Alternatives.pdf
Comparison Table of DiskWarrior Alternatives.pdf
Andrey Yasko
 
Understanding Insider Security Threats: Types, Examples, Effects, and Mitigat...
Understanding Insider Security Threats: Types, Examples, Effects, and Mitigat...Understanding Insider Security Threats: Types, Examples, Effects, and Mitigat...
Understanding Insider Security Threats: Types, Examples, Effects, and Mitigat...
Bert Blevins
 
Tirana Tech Meetup - Agentic RAG with Milvus, Llama3 and Ollama
Tirana Tech Meetup - Agentic RAG with Milvus, Llama3 and OllamaTirana Tech Meetup - Agentic RAG with Milvus, Llama3 and Ollama
Tirana Tech Meetup - Agentic RAG with Milvus, Llama3 and Ollama
Zilliz
 
The Evolution of Remote Server Management
The Evolution of Remote Server ManagementThe Evolution of Remote Server Management
The Evolution of Remote Server Management
Bert Blevins
 
How to build a generative AI solution A step-by-step guide (2).pdf
How to build a generative AI solution A step-by-step guide (2).pdfHow to build a generative AI solution A step-by-step guide (2).pdf
How to build a generative AI solution A step-by-step guide (2).pdf
ChristopherTHyatt
 
Evolution of iPaaS - simplify IT workloads to provide a unified view of data...
Evolution of iPaaS - simplify IT workloads to provide a unified view of  data...Evolution of iPaaS - simplify IT workloads to provide a unified view of  data...
Evolution of iPaaS - simplify IT workloads to provide a unified view of data...
Torry Harris
 
Applying Retrieval-Augmented Generation (RAG) to Combat Hallucinations in GenAI
Applying Retrieval-Augmented Generation (RAG) to Combat Hallucinations in GenAIApplying Retrieval-Augmented Generation (RAG) to Combat Hallucinations in GenAI
Applying Retrieval-Augmented Generation (RAG) to Combat Hallucinations in GenAI
ssuserd4e0d2
 
Password Rotation in 2024 is still Relevant
Password Rotation in 2024 is still RelevantPassword Rotation in 2024 is still Relevant
Password Rotation in 2024 is still Relevant
Bert Blevins
 
Acumatica vs. Sage Intacct vs. NetSuite _ NOW CFO.pdf
Acumatica vs. Sage Intacct vs. NetSuite _ NOW CFO.pdfAcumatica vs. Sage Intacct vs. NetSuite _ NOW CFO.pdf
Acumatica vs. Sage Intacct vs. NetSuite _ NOW CFO.pdf
BrainSell Technologies
 
The Role of IoT in Australian Mobile App Development - PDF Guide
The Role of IoT in Australian Mobile App Development - PDF GuideThe Role of IoT in Australian Mobile App Development - PDF Guide
The Role of IoT in Australian Mobile App Development - PDF Guide
Shiv Technolabs
 

Recently uploaded (20)

find out more about the role of autonomous vehicles in facing global challenges
find out more about the role of autonomous vehicles in facing global challengesfind out more about the role of autonomous vehicles in facing global challenges
find out more about the role of autonomous vehicles in facing global challenges
 
Using LLM Agents with Llama 3, LangGraph and Milvus
Using LLM Agents with Llama 3, LangGraph and MilvusUsing LLM Agents with Llama 3, LangGraph and Milvus
Using LLM Agents with Llama 3, LangGraph and Milvus
 
BT & Neo4j: Knowledge Graphs for Critical Enterprise Systems.pptx.pdf
BT & Neo4j: Knowledge Graphs for Critical Enterprise Systems.pptx.pdfBT & Neo4j: Knowledge Graphs for Critical Enterprise Systems.pptx.pdf
BT & Neo4j: Knowledge Graphs for Critical Enterprise Systems.pptx.pdf
 
“Deploying Large Language Models on a Raspberry Pi,” a Presentation from Usef...
“Deploying Large Language Models on a Raspberry Pi,” a Presentation from Usef...“Deploying Large Language Models on a Raspberry Pi,” a Presentation from Usef...
“Deploying Large Language Models on a Raspberry Pi,” a Presentation from Usef...
 
Amul milk launches in US: Key details of its new products ...
Amul milk launches in US: Key details of its new products ...Amul milk launches in US: Key details of its new products ...
Amul milk launches in US: Key details of its new products ...
 
High Profile Girls Call ServiCe Hyderabad 0000000000 Tanisha Best High Class ...
High Profile Girls Call ServiCe Hyderabad 0000000000 Tanisha Best High Class ...High Profile Girls Call ServiCe Hyderabad 0000000000 Tanisha Best High Class ...
High Profile Girls Call ServiCe Hyderabad 0000000000 Tanisha Best High Class ...
 
Choose our Linux Web Hosting for a seamless and successful online presence
Choose our Linux Web Hosting for a seamless and successful online presenceChoose our Linux Web Hosting for a seamless and successful online presence
Choose our Linux Web Hosting for a seamless and successful online presence
 
Recent Advancements in the NIST-JARVIS Infrastructure
Recent Advancements in the NIST-JARVIS InfrastructureRecent Advancements in the NIST-JARVIS Infrastructure
Recent Advancements in the NIST-JARVIS Infrastructure
 
Dublin_mulesoft_meetup_Mulesoft_Salesforce_Integration (1).pptx
Dublin_mulesoft_meetup_Mulesoft_Salesforce_Integration (1).pptxDublin_mulesoft_meetup_Mulesoft_Salesforce_Integration (1).pptx
Dublin_mulesoft_meetup_Mulesoft_Salesforce_Integration (1).pptx
 
WPRiders Company Presentation Slide Deck
WPRiders Company Presentation Slide DeckWPRiders Company Presentation Slide Deck
WPRiders Company Presentation Slide Deck
 
Comparison Table of DiskWarrior Alternatives.pdf
Comparison Table of DiskWarrior Alternatives.pdfComparison Table of DiskWarrior Alternatives.pdf
Comparison Table of DiskWarrior Alternatives.pdf
 
Understanding Insider Security Threats: Types, Examples, Effects, and Mitigat...
Understanding Insider Security Threats: Types, Examples, Effects, and Mitigat...Understanding Insider Security Threats: Types, Examples, Effects, and Mitigat...
Understanding Insider Security Threats: Types, Examples, Effects, and Mitigat...
 
Tirana Tech Meetup - Agentic RAG with Milvus, Llama3 and Ollama
Tirana Tech Meetup - Agentic RAG with Milvus, Llama3 and OllamaTirana Tech Meetup - Agentic RAG with Milvus, Llama3 and Ollama
Tirana Tech Meetup - Agentic RAG with Milvus, Llama3 and Ollama
 
The Evolution of Remote Server Management
The Evolution of Remote Server ManagementThe Evolution of Remote Server Management
The Evolution of Remote Server Management
 
How to build a generative AI solution A step-by-step guide (2).pdf
How to build a generative AI solution A step-by-step guide (2).pdfHow to build a generative AI solution A step-by-step guide (2).pdf
How to build a generative AI solution A step-by-step guide (2).pdf
 
Evolution of iPaaS - simplify IT workloads to provide a unified view of data...
Evolution of iPaaS - simplify IT workloads to provide a unified view of  data...Evolution of iPaaS - simplify IT workloads to provide a unified view of  data...
Evolution of iPaaS - simplify IT workloads to provide a unified view of data...
 
Applying Retrieval-Augmented Generation (RAG) to Combat Hallucinations in GenAI
Applying Retrieval-Augmented Generation (RAG) to Combat Hallucinations in GenAIApplying Retrieval-Augmented Generation (RAG) to Combat Hallucinations in GenAI
Applying Retrieval-Augmented Generation (RAG) to Combat Hallucinations in GenAI
 
Password Rotation in 2024 is still Relevant
Password Rotation in 2024 is still RelevantPassword Rotation in 2024 is still Relevant
Password Rotation in 2024 is still Relevant
 
Acumatica vs. Sage Intacct vs. NetSuite _ NOW CFO.pdf
Acumatica vs. Sage Intacct vs. NetSuite _ NOW CFO.pdfAcumatica vs. Sage Intacct vs. NetSuite _ NOW CFO.pdf
Acumatica vs. Sage Intacct vs. NetSuite _ NOW CFO.pdf
 
The Role of IoT in Australian Mobile App Development - PDF Guide
The Role of IoT in Australian Mobile App Development - PDF GuideThe Role of IoT in Australian Mobile App Development - PDF Guide
The Role of IoT in Australian Mobile App Development - PDF Guide
 

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