在以下代码中,expiringContentsAllBU
填充在getOnlyContentWhatsNewReport
内的异步/等待函数array.map()
中,但是expiringContentsAllBU
在{{ 1}}函数。
businessUnits.map()
答案 0 :(得分:2)
var getOnlyContentWhatsNewReport = Promise.resolve(123);
const getExpiringContents = async () => {
let businessUnits = [{ id: 1 }, { id: 2 }, { id: 3 }];
const expiringContentsAllBU = await Promise.all(businessUnits.map(async (bu) => {
return getOnlyContentWhatsNewReport.then(respBu => {
bu.businessUnit = respBu;
return bu;
}).catch((err) => {
console.log('No expiring contents found for WhatsNewSection');
return null;
});
}));
console.log('result', expiringContentsAllBU);
}
getExpiringContents();
您必须等到映射完成并且所有回调都完成后。 console.log
和后续代码块将在映射完成之前执行,所以
const getExpiringContents = async () => {
let businessUnits = Object.keys(contentFulSpaces);
const expiringContentsAllBU = await Promise.all(businessUnits.map(async (bu) => {
return getOnlyContentWhatsNewReport(bu, (err, result) => {
if(result) {
let expiringContentsByBU = {};
expiringContentsByBU['businessUnit'] = bu;
expiringContentsByBU['expiringContents'] = result;
return JSON.parse(JSON.stringify(expiringContentsByBU);
} else {
console.log('No expiring contents found for WhatsNewSection');
return null;
}
})
}));
console.log('result', expiringContentsAllBU);
}
答案 1 :(得分:1)
由于map
不了解异步功能,因此您需要使用异步功能。一个示例是等效的Bluebird Promise.map
:
const getExpiringContents = async () => {
let businessUnits = Object.keys(contentFulSpaces);
// Use Promise.map here to convert each businessUnit entry into a record
let expiringContentsAllBU = await Promise.map(businessUnits, async (bu) => {
await getOnlyContentWhatsNewReport(bu, (err, result) => {
if (!result) {
console.log('No expiring contents found for WhatsNewSection');
return;
}
let expiringContentsByBU = {};
expiringContentsByBU['businessUnit'] = bu;
expiringContentsByBU['expiringContents'] = result;
return JSON.parse(JSON.stringify(expiringContentsByBU));
})
});
// As this may contain undefined elements, remove those
expiringContentsAllBU = expiringContentsAllBU.filter(e => e);
console.log('result', expiringContentsAllBU);
}
如果您使getOnlyContentWhatsNewReport
返回应有的承诺而不是使用回调方法,则可以使此代码更加平坦。 async
不会等待回调方法,因此请确保还返回承诺,否则此代码将无法正确等待。
完全重构的 版本看起来像这样:
const getExpiringContents = async () => {
let businessUnits = Object.keys(contentFulSpaces);
let expiringContentsAllBU = await Promise.map(businessUnits, async businessUnit => {
let expiringContents = await getOnlyContentWhatsNewReport(businessUnit);
if (!expiringContents) {
console.log('No expiring contents found for WhatsNewSection');
return;
}
// Choosing names that match the output means you can use the destructuring operator
let expiringContentsByBU = {
businessUnit,
expiringContents
};
// A more formalized "clone" function could help here.
return JSON.parse(JSON.stringify(expiringContentsByBU));
});
// As this may contain undefined elements, remove those
expiringContentsAllBU = expiringContentsAllBU.filter(e => e);
console.log('result', expiringContentsAllBU);
}
答案 2 :(得分:0)
您可以将.map()
更改为for loop
。
或者在您的.map()
函数中,返回承诺。然后,您可以致电await Promise.all(promiseArray)
答案 3 :(得分:0)
使用带有lodash函数的异步来保持理智-
getExpiringContents = async() => {
let businessUnits = Object.keys(contentFulSpaces);
let expiringContentsAllBU = await Promise.map(businessUnits, async businessUnit => {
let expiringContents = await getOnlyContentWhatsNewReport(businessUnit);
if (_.isEmpty(expiringContents)) {
console.log('No expiring contents found for WhatsNewSection');
return;
}
// Choosing names that match the output means you can use the destructuring operator
let expiringContentsByBU = {
businessUnit,
expiringContents
};
// A more formalized "clone" function could help here.
return _.cloneDeep(expiringContentsByBU);
});
// As this may contain undefined elements, remove those
expiringContentsAllBU = _.compact(expiringContentsAllBU);
console.log('result', expiringContentsAllBU);
}