我是JavaScript概念的新手。 爲什麼我得到不同的輸出爲相同的變量長度? 雖然它在體內顯示預期的結果?無法獲得長度
var length;
element.all(by.className("className")).getText().then(function(items){
length = items.length;
console.log("1st output = "+length);//1
console.log("2nd output = "+items.length);//1
});
console.log("3rd output = "+length);//undefined
輸出: - 1輸出= 1個
第二輸出= 1個
3個輸出=不確定
3rd未定義,因爲它是異步調用。 –
'.then'以異步方式工作,當'.then'裏面的代碼執行了語句'console.log(「3rd output =」+ length)''已經完成它的執行並且當時'length'是undefined ..你可以檢查這個鏈接 https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call – brk