适当的异步/等待功能

大卫·冈萨雷斯

我正在尝试运行一个机器人(使用 amazon-buddy)为某些产品(使用 ASIN 数组)抓取亚马逊并检查价格。如果价格不为 0,则应发送不和谐消息。我目前将此设置为每 30 秒运行一次并且它正在工作,但有时似乎每个元素都没有等待前一个元素在 forEach 循环中获得响应,而且我的函数似乎不正确(我仍在尝试正确理解 async/await 功能)。

有没有更好的方法来运行它,以便每个元素在移动到下一个元素之前等待前一个元素被刮掉,然后在 30 秒后再次运行循环?

(function() {
  var c = 0;
  var timeout = setInterval(function() {
      const checkStock = (async () => {
        config.items.itemGroup.forEach(element => {
          console.log('Checking stock on ' + element)
        try {
          const product_by_asin = await amazonScraper.asin({ asin: element });
          console.log(product_by_asin)
          const price = product_by_asin.result[0].price.current_price
          const symbol = product_by_asin.result[0].price.symbol
          const asin = product_by_asin.result[0].asin
          const title = product_by_asin.result[0].title
          const url = product_by_asin.result[0].url
          const image = product_by_asin.result[0].main_image

          if (price != 0) {
            const inStockResponse = {
              color: 0x008000,
              title: title + ' is in stock!',
              url: url,
              author: {
                name: config.botName,
                icon_url: config.botImg,
                url: config.botUrl
              },
              description: '<@767456705306165298>, click the tite to go purchase!\n\n' +
              'Price: ' + symbol + price,
              thumbnail: {
                url: image
              },
              timestamp: new Date()
              }
        
            message.channel.send({embed: inStockResponse });
            console.log(title + ' (' + asin + ') IS available!')
          } else {
            console.log(title + ' (' + asin + ') IS NOT available!')
          }
        } catch (error) {
          console.log(error);
        }
      });
      checkStock()
    });
    console.log('Counter: ' + c)
    c++;
  }, 30000);
})();
多米尼克

您可以使用for...of可以等待每次迭代完成的循环:

async function checkItems(items) {
  // Check all items, wait for each to complete.
  for (const item of items) {
    try {
      const product_by_asin = await amazonScraper.asin({ asin: item });
      console.log(product_by_asin);
      const price = product_by_asin.result[0].price.current_price;
      const symbol = product_by_asin.result[0].price.symbol;
      const asin = product_by_asin.result[0].asin;
      const title = product_by_asin.result[0].title;
      const url = product_by_asin.result[0].url;
      const image = product_by_asin.result[0].main_image;

      if (price != 0) {
        const inStockResponse = {
          color: 0x008000,
          title: title + " is in stock!",
          url: url,
          author: {
            name: config.botName,
            icon_url: config.botImg,
            url: config.botUrl,
          },
          description:
            "<@767456705306165298>, click the tite to go purchase!\n\n" +
            "Price: " +
            symbol +
            price,
          thumbnail: {
            url: image,
          },
          timestamp: new Date(),
        };

        // NOTE: you might want to wait for this too, the error
        // currently isn't being handled like this either.
        message.channel.send({ embed: inStockResponse });
        console.log(title + " (" + asin + ") IS available!");
      } else {
        console.log(title + " (" + asin + ") IS NOT available!");
      }
    } catch (err) {
      console.log(err);
    }
  }

  // Wait 30s and check again.
  setTimeout(() => checkItems(items), 30000);
}

checkItems(config.items.itemGroup);

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章