Instances for ECMAScript Callback Function in Module
ECMAScript Module Essentials – Part 2
Writing ECMAScript Module
Foreword: In this part of the series, I explain how to create callback function instances in the module to serve the same call in the script multilpe times, with different feedback objects.
By: Chrysanthus Date Published: 24 Jul 2016
Consider the following script (read through it):
mod = require('./Modu.js');
This script calls the function, question() in the module three times. question() is typed three times in the script. The first argument of the three calls are different, but the module will send back three different values without knowing the values of the arguments in the three question() calls.
Each question() function call has the same callback function definition. It is the callback function definition in the different question() function calls that display the feedback (that are different) from the module.
Any callback function definition, like these, is called from the module and not from the script.
In order for the module to respond to the three calls of the question() functions in the script, it needs three instances of the callback function calls: the first instance for the first typed question call; the second instance for the second typed question call; and the third instance for the third typed question call.
The module code is (read through it):
var callbackInstancesArr = new Array();
var feedbackValue = 1;
exports.question = function(arg1, callback)
if (callbackInstancesArr != undefined)
feedbackValue = feedbackValue + 1;
The module code begins with the creation of the array, callbackInstancesArr. Whenever, the module question function definition is called from the script, the callback function call is pushed to this array.
feedbackValue holds the feednack value to the script. Initially, it is set to 1. It is incremented for each question function definition evaluation, to have 1, 2 and 3, for the different calls. If you try the different code samples together, you will have the output:
These values are independent of the arguments, 'one', 'two' and 'three' for the different question() function calls.
In the question function definition in the module, the first statement pushes the callback function call to the array. In the scheme, this functuin definition is called three times (from the script).
The second code segment in the definition of the question function, calls the callback function defined in the script with:
Note the use of the array. Each callback function call instance, is an element in the array. After the callback function is called, the code segment removes the element from the array. It then increments the feedback value.
Note that in the question function definition, the first argument has not been used.
The callback function is defined in the script but called from the module. In the script, the sequences of statements typed, having the callback function definiton, correspond to different instances (array elements) of the callback() function calls, in the module.
That is it for this part of the series.
Related LinksInternet Socket and ECMAScript
Handling Bytes in ECMAScript
ECMAScript Bitwise Operators
ECMAScript Module Essentials
More Related Links
Node.js Web Development Course
Major in Website Design
Low Level Programming - Writing ECMAScript Module