如何Promise.all对于嵌套数组?

时间:2018-10-25 15:51:28

标签: javascript arrays asynchronous promise bluebird

数据结构:

tasks: [
  {
    name: "get milk",
    users: ["abc", "def"]
  },
  {
    name: "buy bread",
    users: ["def", "ghi"]
  }
]

我需要从数据库中获取每个用户的电子邮件地址(到目前为止非常好),并等待所有任务完成,然后继续进行数据处理。无效的地方写在下面的注释中:

var tasks_with_emails = tasks.map(function(task) {
  var emails = task.users.map(function(user) {
    return user_to_email(user); // from the database
  });
  Promise.all(emails).then(function(emails) {
    task.emails = emails;
    console.log(task); // this one works fine
    return task;
  }).catch(next);
});
Promise.all(tasks_with_emails).then(function(tasks) {
  console.log(tasks); // <==== this one fires too quickly
}).catch(next);

因此tasks_with_email应该等待所有嵌套的emails首先解析,但不是。

1 个答案:

答案 0 :(得分:0)

一旦您在Promise.all()前面添加了退货,您的代码就对我有用,如下面的示例所示。 因此,请看一下user_to_email()会返回什么。这应该是一个可以解析为电子邮件字符串的承诺。

const user_to_email = user => new Promise(( resolve, reject ) => {
  setTimeout(() => resolve( `${ user }@example.com` ), 3000 );
});
const tasks  = [
  {
  name: "get milk",
  users: ["abc", "def"]
  },
  {
  name: "buy bread",
  users: ["def", "ghi"]
  }
];
const tasks_with_emails = tasks.map( task => {
  const emails = task.users.map( user => {
    return user_to_email(user); // from the database
  });
  return Promise.all(emails).then( emails => {
    task.emails = emails;
    return task;
  });
});
Promise.all(tasks_with_emails).then( tasks => {
  console.log(tasks); // <==== this one fires too quickly
});
setTimeout(() => console.log( '1000ms' ), 1000 );
setTimeout(() => console.log( '2000ms' ), 2000 );
setTimeout(() => console.log( '2999ms' ), 2999 );