Javascript 承诺:链接方法并处理两个返回的对象?

Javascript 承诺:链接方法并处理两个返回的对象?,javascript,es6-promise,Javascript,Es6 Promise,我有以下代码: return (entity.User.getProjects(req.user.id, ....)) .then((list) => { // party with list object! return { list: list } } 我还想调用另一个方法,entity.Tag.getAllForUser,它返回一个tags列表 (更新:我只需要使用承诺就可以做到这一点-我不能使

我有以下代码:

return (entity.User.getProjects(req.user.id, ....))
    .then((list) => {
        // party with list object!
        return {
            list: list
        }
    }
我还想调用另一个方法,
entity.Tag.getAllForUser
,它返回一个
tags
列表

(更新:我只需要使用承诺就可以做到这一点-我不能使用async/await。)

此代码不起作用-它表示
list
tag
都未定义:

return (entity.User.getProjects(req.user.id, ....))
    .then((list) => entity.Tag.getAllForUser(req.user.id))
    .then((list, tags) => {
        // party with both list and tags objects!
        return {
            list: list,
            tags: tags
        }
    }
我还尝试将第二次呼叫链接到第一次呼叫:

return (
    entity.User.getProjects(req.user.id, ....).then((list) => 
    entity.Tag.getAllForUser(req.user.id))
 ).then((list, tags) => {
    // party with both list and tags objects!
    return {
        list: list,
        tags: tags
    }
}

但它再次指出,
list
是未定义的。什么有效?

回答我自己的问题。请不要删除,因为这可能对其他人有用

return (entity.User.getProjects(req.user.id, ....))
    .then((list) => Promise.all([list, entity.Tag.getAllForUser(req.user.id)]))
    .then(([list, tags]) => {
        // party with both list and tags objects!
        return {
            list: list,
            tags: tags
        }
}

只需使用
Async/Await

let list = await entity.User.getProjects(req.user.id, ....))
let tags = await entity.Tag.getAllForUser(req.user.id))

return {
        list: list,
        tags: tags
    }
}
如果您不能使用
Async/Await
(尽管我建议使用它们),您可以使用promise.all

Promise.all([
   entity.User.getProjects(req.user.id),
   entity.Tag.getAllForUser(req.user.id);
 ]).then(([list,tag])=>{
   // do here whatever you want
})

这里最好的解决方案是
Promise.all
,它获取承诺列表,并返回解析值列表:

return Promise.all([
  entity.User.getProjects(req.user.id),
  entity.Tag.getAllForUser(req.user.id);
])
  .then(([ list, tags ]) => {
    // Party!
    return { list, tags };
  });
使用
async
wait
的强制性建议:

let [ list, tags ] = await Promise.all([
  entity.User.getProjects(req.user.id),
  entity.Tag.getAllForUser(req.user.id);
]);

return { list, tags };

这取决于您的请求是否独立:

在独立请求上获取多个已解析的值: 假设我有
getUsers()
getCities()
,两者都是独立的(您不需要用户来获取cities和viceversa)。然后您可以使用
承诺。所有

Promise.all([getUsers(), getCities()])
.then(result => {
  // result here will be an array of two elements:
  // result[0] -> what getUsers resolves
  // result[1] -> what getCities resolves 
});
获取依赖请求的多个已解析值: 比如说,如果您有
getUser()
,它返回一个用户,然后是
getCouple(user)
,它返回一个与用户相关的人,然后是
getHouse(user1,user2)
,恰好两者都需要,那么您可以执行以下操作:

const user = getUser();
const couple = user.then(u => getCouple(u)); // you can reuse a promise. This does not trigger then handler again, if
// the promise is settled will yield the settle value.
const house = user.then(u => {
  return couple.then(c => {
    return getHouse(u,c);
  });
});
使用async/await会更好:
async function() {
  const user = await getUser();
  const couple = await getCouple(user);
  const house = await getHouse(user, couple);
}

为什么你有多重回报?@chevybow,因为我做错了。你的标题是“链方法”,但你为什么需要链呢?@trincot可能我不需要!我需要执行这两个命令,并使用这两个命令的结果。对不起,没有说得更准确。谢谢!这将是最简单的方法,但不幸的是我不能这样做,因为我工作的代码库不允许这样做。(已更新问题以澄清此问题。)@Richard您可以使用
promise.all
,尽管我仍然建议使用
asyc/await
,这使代码更易于阅读和维护。我建议使用
promise.all
同时运行两个查询!您的解决方案将不必要地连续运行。谢谢!这是我在任何地方见过的最清楚的解释:)