最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

异步等待所有承诺

IT培训 admin 14浏览 0评论

异步等待所有承诺

我想知道我是否在异步等待中正确使用了promise.all。

[基本上,我需要根据ID获取房屋数据,然后我需要获取该房屋的所有评论以及评论数。

  server.get("/api/houses/:id", async (req, res) => {
    const { id } = req.params;
    const house = await House.findByPk(id);
    if (!house) {
      return res.status(400).send("No house found");
    }

    const reviews = await Review.findAndCountAll({
      where: {
        houseId: house.id
      }
    });

    house.dataValues.reviewsCount = reviews.count;

    const results = await Promise.all([house.dataValues, reviews.rows]);
    console.log(results);
    res.send(results);
  });

在发出http请求后,当我console.log响应时,在前端,我返回下面的内容,因为Promise.all为您提供了数组。但是我不知道这是否是最好的方法,或者是否有更好的方法。

[
  {
    id: 2329,
    host: 2,
    picture: '/img/houses/1.jpg',
    type: 'Entire house',
    town: 'Some town',
    title: 'Some title',
    price: 50,
    description: 'Some description',
    guests: 4,
    bedrooms: 1,
    beds: 2,
    baths: 1,
    wifi: true,
    reviewsCount: 2
  },
  [
    {
      id: 1,
      houseId: 2329,
      userId: 1,
      comment: 'An awesome review',
      createdAt: '2019-01-11T22:00:00.000Z',
      updatedAt: '2019-01-11T22:00:00.000Z'
    },
    {
      id: 2,
      houseId: 2329,
      userId: 2,
      comment: 'Another awesome review',
      createdAt: '2019-01-11T22:00:00.000Z',
      updatedAt: '2019-01-11T22:00:00.000Z'
    }
  ]
]
回答如下:

您没有正确使用Promise.all。该代码有效,因为您要分别await兑现每个诺言。

由于Review.findAndCountAll取决于House.findByPk结果,因此Promise.all在这里不会有任何好处。

您正在将Promise.all与两个promise的已解析值一起使用,因此可以将其删除。

 server.get("/api/houses/:id", async (req, res) => {
    const { id } = req.params;
    const housePromise = await House.findByPk(id);


    const reviews = await Review.findAndCountAll({
      where: {
        houseId: house.id
      }
    });

    house.dataValues.reviewsCount = reviews.count;

    res.send([house.dataValues, reviews.rows]);
 });

基本上是您正在做的:

const res = await Promise.all([1, 5]); // [1, 5]

可以直接翻译成:

const res = [1, 5];

而不是将其发送到数组中,我认为最好发送一个对象:

{
   house: house.dataValues,
   reviews: reviews.rows
}

异步等待所有承诺

我想知道我是否在异步等待中正确使用了promise.all。

[基本上,我需要根据ID获取房屋数据,然后我需要获取该房屋的所有评论以及评论数。

  server.get("/api/houses/:id", async (req, res) => {
    const { id } = req.params;
    const house = await House.findByPk(id);
    if (!house) {
      return res.status(400).send("No house found");
    }

    const reviews = await Review.findAndCountAll({
      where: {
        houseId: house.id
      }
    });

    house.dataValues.reviewsCount = reviews.count;

    const results = await Promise.all([house.dataValues, reviews.rows]);
    console.log(results);
    res.send(results);
  });

在发出http请求后,当我console.log响应时,在前端,我返回下面的内容,因为Promise.all为您提供了数组。但是我不知道这是否是最好的方法,或者是否有更好的方法。

[
  {
    id: 2329,
    host: 2,
    picture: '/img/houses/1.jpg',
    type: 'Entire house',
    town: 'Some town',
    title: 'Some title',
    price: 50,
    description: 'Some description',
    guests: 4,
    bedrooms: 1,
    beds: 2,
    baths: 1,
    wifi: true,
    reviewsCount: 2
  },
  [
    {
      id: 1,
      houseId: 2329,
      userId: 1,
      comment: 'An awesome review',
      createdAt: '2019-01-11T22:00:00.000Z',
      updatedAt: '2019-01-11T22:00:00.000Z'
    },
    {
      id: 2,
      houseId: 2329,
      userId: 2,
      comment: 'Another awesome review',
      createdAt: '2019-01-11T22:00:00.000Z',
      updatedAt: '2019-01-11T22:00:00.000Z'
    }
  ]
]
回答如下:

您没有正确使用Promise.all。该代码有效,因为您要分别await兑现每个诺言。

由于Review.findAndCountAll取决于House.findByPk结果,因此Promise.all在这里不会有任何好处。

您正在将Promise.all与两个promise的已解析值一起使用,因此可以将其删除。

 server.get("/api/houses/:id", async (req, res) => {
    const { id } = req.params;
    const housePromise = await House.findByPk(id);


    const reviews = await Review.findAndCountAll({
      where: {
        houseId: house.id
      }
    });

    house.dataValues.reviewsCount = reviews.count;

    res.send([house.dataValues, reviews.rows]);
 });

基本上是您正在做的:

const res = await Promise.all([1, 5]); // [1, 5]

可以直接翻译成:

const res = [1, 5];

而不是将其发送到数组中,我认为最好发送一个对象:

{
   house: house.dataValues,
   reviews: reviews.rows
}

与本文相关的文章

发布评论

评论列表 (0)

  1. 暂无评论