Broad Network


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

Introduction

This is part 2 of my series, ECMAScript Module Essentials. 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. You should have read the previous part of the series before coming here, as this is the continustion.

Example Script
Consider the following script (read through it):

mod = require('./Modu.js');

    mod.question('one', function(arg)
        {
            console.log(arg);
        });

    mod.question('two', function(arg)
        {
            console.log(arg);
        });

    mod.question('three', function(arg)
        {
            console.log(arg);
        });


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.

Module Code
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)
        {

            callbackInstancesArr.push(callback);


            if (callbackInstancesArr[0] != undefined)
                {
                    callbackInstancesArr[0](feedbackValue);
                    callbackInstancesArr.shift();
                    feedbackValue = feedbackValue + 1;
                }

        }

The feedback value to the different question() function calls is 1 for the first call, 2 for the second call, and 3 for the third call. These values are independent of the first argument of the question() function calls.

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:

    1
    2
    3

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:

        callbackInstancesArr[0](feedbackValue);

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.

Conclusion
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.

Chrys

Related Links

Internet Socket and ECMAScript
Handling Bytes in ECMAScript
ECMAScript Bitwise Operators
ECMAScript Module Essentials
More Related Links
Node Mailsend
EMySQL API
Node.js Web Development Course
Major in Website Design
Low Level Programming - Writing ECMAScript Module
ECMAScript Course

BACK

Comments

Become the Writer's Follower
Send the Writer a Message