1

Note the second function block first to get a better understanding.

So basically I have a large array of "tags" (variables) that are iterated through at the "for (let tag of tagArray) {...}" statement. The values of each of these "tags" is requested from a server asynchronously at "tagFunctionality.addGenericTag(tag.watchTag, function(value) {...}".

As I am receiving a lot of data, this process can become quite slow; to improve efficiency I proposed cancelling requests that I know will return a zero. After roughly ~1000 values the callback values start to become zeros in large chunks so I assume I can ignore these chunks and just default to zero instead of interpreting a zero value from the server, leading to a faster completion time. Currently I am forced to iterate through every value and wait for the server to return every value before proceeding. How can I successfully stop receiving particular asynchronous calls or something similar/more efficient?

(See my partial attempt at the "MY RECENT EDITION" comment.)

Function 1:

var repetitionLog = [];
var addGenericTag = function(tag, callback) {
    // this spot runs before bulk data is received
    currentSession.readVariableValue("ns=2;s=" + tag, function(err, dataValue) {
            // this spot runs after bulk data is received
            if (dataValue && dataValue.value) {

                // MY RECENT EDITION: Detect chain of zeros
                if (dataValue.value.value == 0) {
                     repetitionLog.push(dataValue.value.value)
                     // console.log(repetitionLog.length);
                     if (repetitionLog.length >= 1000) {
                         // this spot runs if 1000 zeros have been returned
                         // skip the next 100 callbacks and reset the repetition log or something
                     }
                }

                callback(dataValue.value.value);
            } else {
                callback(null);
            }
    });
}

Function 2:

var tagArray = []; // pretend this array is full of tags
var generateTagSystem = function(tagArray) {
    var tagSystem = function() {
        // Connect to this server
        tagFunctionality.connect(function() {
            // Subscribe to created session
            tagFunctionality.start();

            // IMPORTANT BIT, iterates over thousands of tags
            for (let tag of tagArray) {
                // Request value object of a particular tag
                tagFunctionality.addGenericTag(tag.watchTag, function(value) {
                    // Tag is empty
                    if (value === null) {
                      // Tag value is empty, error code
                      // Tag is not empty or zero
                    } else if (value != 0) {
                      // Do something with tag code
                      // Tag is zero
                    } else {
                      // possibly useful future spot
                    }
                });
            }
        });
    };
    tagSystem();
};
About7Deaths
  • 681
  • 1
  • 5
  • 16
  • Possible duplicate of [Cancel a vanilla ECMAScript 6 Promise chain](https://stackoverflow.com/questions/29478751/cancel-a-vanilla-ecmascript-6-promise-chain) – Ian Jun 13 '19 at 18:30

0 Answers0