I am using Bluebird.js and request-promise NPM module. I want to access promise URL or item.transactionID as in the code below. I try to find many things but failed to work How can we achieve this.
paymentIDArray.forEach(item => {
let p = rp({
uri: API + item.transactionID,
headers: {
"Content-Type": "application/json",
"Authorization": "Basic " + authCode
},
simple: false,
resolveWithFullResponse: false,
transform2xxOnly: false
}).promise();
promises.push(p);
});
await Promise
.all(promises)
.each(async (inspection) => {
if (inspection.isFulfilled()) {
// I want item.transactionID of each promise here
let result = JSON.parse(inspection.value());
} else {
logger.error(inspection);
logger.error("A promise in the array was rejected with", inspection.reason());
}
});
You can add item.id
to the promise value you push
paymentIDArray.forEach(item => {
let p = rp({
uri: API + item.transactionID,
headers: {
"Content-Type": "application/json",
"Authorization": "Basic " + authCode
},
simple: false,
resolveWithFullResponse: false,
transform2xxOnly: false
}).promise();
promises.push(p.then(inspection => ({inspection, id: item.id})));
});
// each promise, instead of being just the result of `rp` is now {inspection: result of rp, id: item.id}
await Promise
.all(promises)
// vvvvv do you really need async? you are not awaiting in this code
.each(async ({inspection, id}) => {
if (inspection.isFulfilled()) {
// id is the required item.id
let result = JSON.parse(inspection.value());
} else {
logger.error(inspection);
logger.error("A promise in the array was rejected with", inspection.reason());
}
});