Navigation

    Logo
    • Register
    • Login
    • Search
    • Recent
    • Tags
    • Unread
    • Categories
    • Unreplied
    • Popular
    • GitHub
    • Docu
    • Hilfe
    1. Home
    2. Deutsch
    3. Skripten / Logik
    4. JavaScript
    5. [gelöst/Pebcak] Problem mit Klasse/Zugriff auf variable

    NEWS

    • Neuer Blog: Fotos und Eindrücke aus Solingen

    • ioBroker@Smart Living Forum Solingen, 14.06. - Agenda added

    • ioBroker goes Matter ... Matter Adapter in Stable

    [gelöst/Pebcak] Problem mit Klasse/Zugriff auf variable

    This topic has been deleted. Only users with topic management privileges can see it.
    • Great SUN
      Great SUN last edited by Great SUN

      Hi Ihr, ich verstehe etwas nicht... Vielleicht kann mir das jemand erklären, oder mir evtl. auch bei einer Lösung für das Problem, das ich mit der Klasse versuche zu lösen helfen...

      Das zu erreichende Ziel ist, dass ich eine Anzahl (sagen wir 😎 URLs abfragen möchte und wenn mind. 4 davon erfolgreich abgefragt wurden, möchte ich einen zweiten callBack auslösen, der mir dann im callBack2Dict hinterlegte Schalter "umlegt".

      class runParallelHttpAndSetGroupSwitch {
          constructor(urlListObj, options, callBack, callBack2, callBack2Dict) {
              this.urlListObj = urlListObj;
              this.options = options;
              this.callBack = callBack;
              this.callBack2 = callBack2;
              this.callBack2Dict = callBack2Dict;
              this.neededAmount = Math.round(this.urlListObj.length / 2);
              this.callBack2Executed = false;
              this.returnCount = 0;
          }
          execHttpRequest(urlListKey) {
              var request = require('then-request');
              request('GET', this.urlListObj[urlListKey], this.options).getBody('utf-8').done((resBody) => {
                  this.returnCount += 1;
                  if(this.neededAmount > 0) {
                      this.neededAmount -= 1;
                  }
                  if (this.callBack2Executed === false && this.neededAmount == 0) {
                      logStr('Achieved! -> calling callBack2 with ' + JSON.stringify(this.callBack2Dict));
                      this.callBack2Executed = true;
                      this.callBack2(this.callBack2Dict);
                  }
                  var logConfArr = [urlListKey, ' -> parralel request result', this.returnCount, '(', this.neededAmount, ') cb2exec: ', this.callBack2Executed, ' -> @@result@@'];
                  this.callBack(resBody, logConfArr);
              })
          }
          execute() {
              var promiseArr = [];
              Object.keys(this.urlListObj).map((urlListKey) => {
                  promiseArr.push(this.execHttpRequest(urlListKey));
              });
              try {
                  Promise.all(promiseArr).catch(function (error) {
                      logStr('Error-catch1 while executing parallel requests: ' + JSON.stringify(this.urlListObj) + ': ' + error);
                  });
              } catch (error) {
                  var errMsg = 'Error-catch2 while trying to execute parallel requests: ' + JSON.stringify(this.urlListObj) + ': ' + error;
                  logStr(errMsg, 'warn');
              }
          }
      }
      
      async function execParallelHttpRequests(urlListObj, overrideOptions, noMerge, callBack, callBack2, callBack2Dict) {
          var options = { 'retry': true, 'retryDelay': 150, 'maxRetries': 10 };
          options = mergeOptionDict(options, overrideOptions, noMerge);
          var parReqGroupSwitch = new runParallelHttpAndSetGroupSwitch(urlListObj, options, callBack, callBack2, callBack2Dict);
          parReqGroupSwitch.execute();
      }
      

      Bei einer Ausgabe des logConfArr kommt dann folgendes heraus:

      console.log(logConfArr.join())
      
      ->
      
      <request url> parralel request result1(NaN) cb2exec: false -> <request result>
      

      Wieso gibt this.returnCount einen Wert aus, aber this.neededAmount ist NaN?!?

      Danke für Eure Unterstützung!

      P.S.: Kann sein, dass ich das mittlerweile schon wieder total verhunzt hab, aber ich bastel da jetzt schon ne Weile dran herum und ich weiß nicht mehr, was ich noch ändern soll...

      1 Reply Last reply Reply Quote 0
      • Great SUN
        Great SUN last edited by

        Oh man... wie doof muss man sein...
        Ein Dictionary und ich mach .length drauf...

        this.neededAmount = Math.round(Object.keys(this.urlListObj).length / 2);
        

        so gehört das!

        1 Reply Last reply Reply Quote 0
        • First post
          Last post

        Support us

        ioBroker
        Community Adapters
        Donate

        812
        Online

        31.8k
        Users

        80.0k
        Topics

        1.3m
        Posts

        1
        2
        127
        Loading More Posts
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes
        Reply
        • Reply as topic
        Log in to reply
        Community
        Impressum | Datenschutz-Bestimmungen | Nutzungsbedingungen
        The ioBroker Community 2014-2023
        logo