I need to output a long list of shipments and their tracking Status/ tracking URL
.
If I was running this synchronously then it may well take a very long time. So if I run it asynchronously it should be quicker as all the requests will run at the same time, then return all the information to the frontend when it's all done.
So I'm still learning in node and using promises, however I have got this so far... but the problem is that the Promise.all never seems to execute. So I'm not sure if I'm doing the Promise function correctly or is it something I don't know about the easyPost API
?
yes for now I'm just using 2 shipment Id's but it could run to 100's or more...
also is there a way of returning a tracker element when retrieving a shipment, so that I don't have to do 2 calls for each shipment?
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)
})
})
}
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)
})
})
}