Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/450.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Promise.all不使用异步函数数组_Javascript_Ecmascript 6_Promise_Es6 Promise - Fatal编程技术网

Javascript Promise.all不使用异步函数数组

Javascript Promise.all不使用异步函数数组,javascript,ecmascript-6,promise,es6-promise,Javascript,Ecmascript 6,Promise,Es6 Promise,考虑以下代码,理论上,这些代码在I/O操作完成后将消息打印到控制台 const foo = (num) => new Promise(resolve => setTimeout(resolve, num * 1000)); // An async I/O function in actual code array = [[1, 2, 3], [1, 2, 3] , [1, 2, 3]]; const promiseArray = array.map(arr => { ar

考虑以下代码,理论上,这些代码在I/O操作完成后将消息打印到控制台

const foo = (num) => new Promise(resolve => setTimeout(resolve, num * 1000)); // An async I/O function in actual code
array = [[1, 2, 3], [1, 2, 3] , [1, 2, 3]];

const promiseArray = array.map(arr => {
  arr.map(num => {
    return (async () => {
      await foo(num);
      console.log(num);
    });
  });
}).flat();

await Promise.all(promiseArray);
我不知道为什么,但它不起作用。控制台上没有打印任何内容


但是,如果我将异步函数包装在承诺构造函数中,它就会工作

const foo = (num) => new Promise(resolve => setTimeout(resolve, num * 1000)); // An async I/O function in actual code
array = [[1, 2, 3], [1, 2, 3] , [1, 2, 3]];

const promiseArray = array.map(arr => {
  arr.map(num => {
    return new Promise(async () => {
      await foo(num);
      console.log(num);
    });
  });
}).flat();

await Promise.all(promiseArray);

我应该如何重写代码以摆脱Promise构造函数?

您正在从映射回调返回函数,而不是Promise。而是返回
foo(num)
。然后在平坦化之后,你有一系列的承诺

const foo = (num) => new Promise(resolve => setTimeout(resolve, num * 1000)); // An async I/O function in actual code
array = [[1, 2, 3], [1, 2, 3] , [1, 2, 3]];

const promiseArray = array.map(arr => {
  return arr.map(foo); // its equal arr.map(num => foo(num));
}).flat();

const results = await Promise.all(promiseArray);
results.forEach(item => console.log(item));

异步函数必须返回承诺。您的第一个实现需要返回语句:

const array = [[1, 2, 3], [1, 2, 3] , [1, 2, 3]];

const promiseArray = array.map(arr => {
  // returning the mapped list
  return arr.map(async (num) => {
    const result = await foo(num);
    console.log('Num: ' + num);
    // return at the end of async will generate a promise fulfillment for you.
    // see: https://developers.google.com/web/fundamentals/primers/async-functions
    return result;
  });
}).flat();

const result = await Promise.all(promiseArray);
console.log('result: ' + result);

Promise.all
将一个promises数组作为其参数,而不是一个
异步函数数组。此外,您还缺少一条
return
语句。你应该写

const promiseArray = array.flatMap(arr => {
  return arr.map(async num => {
    await foo(num);
    console.log(num);
  });
});

await Promise.all(promiseArray);


它的普通Promise.all接受一个Promise数组,异步函数的类型为function,但一旦调用,就会返回一个Promise,如果没有显式返回,它将返回一个带有未定义值的已解析Promise

异步函数myAsyncFunction(){ 返回1; } console.log(myAsyncFunction的类型) console.log(myAsyncFunction()的类型)
log(myAsyncFunction()instanceof Promise)
有两个问题:1。在第一次回调中,您不返回任何内容,因此
promiseArray
只是
[undefined,undefined,undefined]
。2.即使您放置了一个
返回
,您也不会返回承诺,而是返回一个异步函数数组。你必须执行它们才能得到承诺。你应该返回
arr.map(num=>{
为了生成一个2D数组,然后将其展平。现在这仍然不是生成一个承诺数组,而是生成一个异步函数数组。哦,我在回答中也犯了一个错误,good point@VLAZAn async function总是返回一个承诺。无论您是否使用承诺声明。请参阅:只要调用它,它就会返回一个承诺,这是no这里的调用,只是一个声明console.log语句如何?如果我需要在异步函数中包含更多语句,我应该如何编写它?您可以很容易地做到。正如前面提到的注释和Brunos答案中所述,异步函数应该返回一个承诺。您可以做任何您喜欢的事情,并在最后返回一个承诺(你没有做的工作)。另一个问题是,你必须返回
arr.map(num=>{
(正如我在对Brunos回答的评论中提到的那样),即使你没有从异步函数返回一个承诺,你返回的任何东西都将被包装在
承诺.解决
(promisified)我有点困惑。下面的表达式不是创建了一个异步函数而不是一个承诺吗?async num=>{await foo(num);console.log(num);}@Max Yes,并且
map
调用该异步函数,将
arr
映射到一个承诺数组。我可以将其视为以下语句吗?arr.map((I)=>(async num=>{await foo(num);console.log(num);})(i))@Max Yes,但是使用IIFE是毫无意义的。
const promiseArray = array.map(async arr => {
  await Promise.all(arr.map(async num => {
    await foo(num);
    console.log(num);
  }));
});

await Promise.all(promiseArray);