访问 URL in promise each
Access URL in promise each
我正在使用 Bluebird.js 和请求承诺 NPM 模块。
我想按照下面的代码访问承诺 URL 或 item.transactionID。
我试图找到很多东西,但没有成功
我们怎样才能做到这一点。
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());
}
});
您可以将item.id
添加到您推送的承诺值
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());
}
});
我正在使用 Bluebird.js 和请求承诺 NPM 模块。 我想按照下面的代码访问承诺 URL 或 item.transactionID。 我试图找到很多东西,但没有成功 我们怎样才能做到这一点。
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());
}
});
您可以将item.id
添加到您推送的承诺值
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());
}
});