0

我需要输出一长串货物及其跟踪状态/跟踪URL

如果我同步运行它,那么它可能需要很长时间。因此,如果我异步运行它应该会更快,因为所有请求将同时运行,然后在全部完成后将所有信息返回到前端。

所以我仍在学习节点并使用承诺,但是到目前为止我已经做到了......但问题是 Promise.all 似乎永远不会执行。所以我不确定我是否正确地执行了 Promise 功能,还是我不知道easyPost API

是的,现在我只使用 2 个货件 ID,但它可能会运行到 100 个或更多...

还有一种方法可以在检索货件时返回跟踪器元素,这样我就不必为每批货件打 2 次电话了吗?

var promise = []
promise.push(getShipmentPromise('shp_xxxShipmentId'))
promise.push(getShipmentPromise('shp_xxxShipmentId2'))
Promise.all(promise)
   .then(response => {
      console.log('Sent - ' + req.method + req.originalUrl)
      console.log('promise.all complete.')
      // within an expressjs route
      res.json(batches)
   })
   .catch(err => {
      console.log(err)
   })        

    function getShipmentPromise (shipmentId) {
      return new Promise((resolve, reject) => {
        easyPostAPI.Shipment.retrieve(shipmentId).then(response => {
          console.log(response.created_at)
        })
      })
    }
4

1 回答 1

1

You need to resolve or reject the promise. If you need a tracker element, get that and then resolve.


function getShipmentPromise (shipmentId) {
      return new Promise((resolve, reject) => {
        easyPostAPI.Shipment.retrieve(shipmentId).then(response => {
          console.log(response.created_at)
          resolve(response)
        })
      })
}

于 2019-10-23T09:58:40.357 回答