这个语法是怎么回事? (let results = [], i;) 我从来没有见过两个值赋给一个变量

What is going on with this syntax? (let results = [], i;) I have never seen two values assigned to a variable

我找到了这个如何实现 Promise.all 方法的例子。

function all(iterable){ // take an iterable 
  // `all` returns a promise.  
  return new Promise(function(resolve, reject){ 
    let counter = 0; // start with 0 things to wait for
    let results = [], i;
    for(let p of iterable){
        let current = i;
        counter++; // increase the counter 
        Promise.resolve(p).then(function(res){ // treat p as a promise, when it is ready: 
          results[i] = res; // keep the current result
          if(counter === 0) resolve(results) // we're done
        }, reject); // we reject on ANY error
       i++; // progress counter for results array
    }
  });
}

我不确定这行代码是怎么回事:let results = [], i;

当我 运行 在控制台中 i 变得未定义。在 all 函数中,他们正在执行 i++,但在未定义的值上使用该运算符会变成 NaN。这里发生了什么?如果 'i' 未定义,他们如何将其用作 results[i] = res; // keep the current result 的数组索引?

let results = [], i;

相当于

let results = [];
let i;
let results = [], i;

等同于:

let results = [];
let i;

i 得到值 undefined。 undefined 上的任何数学运算都将 return NaN 不是数字。

console.log(undefined + 1);