Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
364 views
in Technique[技术] by (71.8m points)

node.js - 异步等待所有承诺(async await with promise all)

I want to know if I am using promise.all correctly with async await.

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

Basically, I need to get the house data based on the ID, then I need to get all the reviews for that house as well as the count of reviews.

(基本上,我需要基于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);
  });

In the front end when I console.log the response after making the http request, I get back the below which seems okay since Promise.all gives you arrays.

(在前端,当我在发出http请求后进行console.log响应时,我得到了下面的内容,因为Promise.all为您提供了数组。)

But I don't know if this is the best way to do this or if there is a better way.

(但是我不知道这是否是最好的方法,或者是否有更好的方法。)

[
  {
    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'
    }
  ]
]
  ask by user10980228 translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You're not using Promise.all correctly.

(您没有正确使用Promise.all 。)

The code is working, because you're await ing each promise individually.

(该代码有效,因为您正在await每个诺言。)

Since Review.findAndCountAll depends on House.findByPk result, Promise.all won't do any good here.

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

You're using Promise.all with the already resolved values of the both promises, so you can just drop it.

(您正在使用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]);
 });

Basically you're doing:

(基本上你在做:)

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

Which can be translated directly to:

(可以直接将其翻译为:)

const res = [1, 5];

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...