如何 Promise.all 用于嵌套数组?
How to Promise.all for nested arrays?
数据结构:
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
先解析,但它没有。
在 Promise.all()
前面添加 return 后,您的代码对我有效,如下例所示。
所以看看 user_to_email()
return 是什么。这应该是解析为电子邮件字符串的承诺。
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 );
数据结构:
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
先解析,但它没有。
在 Promise.all()
前面添加 return 后,您的代码对我有效,如下例所示。
所以看看 user_to_email()
return 是什么。这应该是解析为电子邮件字符串的承诺。
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 );