NEWS
[gelöst/Pebcak] Problem mit Klasse/Zugriff auf variable
-
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...
-
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!