Javascript 如何在map函数中使用async和wait进行顺序执行
Hi for me async and await是全新的。我有一个需要await方法的函数调用,所以我需要使用async and await。 我得到了这些值,但它们不符合顺序Javascript 如何在map函数中使用async和wait进行顺序执行,javascript,asynchronous,async-await,Javascript,Asynchronous,Async Await,Hi for me async and await是全新的。我有一个需要await方法的函数调用,所以我需要使用async and await。 我得到了这些值,但它们不符合顺序 async function main() { const items = []; // Fill your items const articles = []; // Fill your articles // Async map function return promise for each ite
async function main() {
const items = []; // Fill your items
const articles = []; // Fill your articles
// Async map function return promise for each item
const promises = items.map(async (item, i) => {
console.log(item.FieldValuesAsText.FileRef);
const itemx = await Promise.all([
w.getFileByServerRelativeUrl(item.FieldValuesAsText.FileRef).getItem()
]);
console.log(item);
var likes;
likes = await Promise.all(itemx.getLikedByInformation());
console.log("like " + likes.likeCount);
const comments = await Promise.all(itemx.comments.get());
console.log("Comments Count " + comments.length);
// Create new object by appending articles[i],likes,comments
return {
...articles[i],
likecount: likes.likeCount,
commentcount: comments.length,
FileRef: item.FieldValuesAsText.FileRef
};
});
// Here you have everything in order.
const newst = await Promise.all(promises);
}
下面是我的异步和等待映射函数
items.map(async (item) =>{
const itemx = await
Promise.all([w.getFileByServerRelativeUrl(item.FieldValuesAsText.FileRef).getItem()]);
var likes = await Promise.all(itemx.getLikedByInformation());
const comments = await Promise.all(itemx.comments.get());
articles[i].likecount = likes.likeCount
articles[i].commentcount = comments.length
articles[i].FileRef = item.FieldValuesAsText.FileRef
newst.push(articles[i++])
})
任何建议都将非常有用您必须等待map函数的承诺,才能按顺序获得结果
async function main() {
const items = []; // Fill your items
const articles = []; // Fill your articles
// Async map function return promise for each item
const promises = items.map(async (item, i) => {
console.log(item.FieldValuesAsText.FileRef);
const itemx = await Promise.all([
w.getFileByServerRelativeUrl(item.FieldValuesAsText.FileRef).getItem()
]);
console.log(item);
var likes;
likes = await Promise.all(itemx.getLikedByInformation());
console.log("like " + likes.likeCount);
const comments = await Promise.all(itemx.comments.get());
console.log("Comments Count " + comments.length);
// Create new object by appending articles[i],likes,comments
return {
...articles[i],
likecount: likes.likeCount,
commentcount: comments.length,
FileRef: item.FieldValuesAsText.FileRef
};
});
// Here you have everything in order.
const newst = await Promise.all(promises);
}
每个map item函数都异步运行,因此不保证map函数内部的顺序,但您可以从map函数返回值,该值可以使用
wait Promise.all()解析为有序数组
因为map函数返回的promise数组的顺序正确。请尝试promise.mapSeries,以便使用异步等待进行顺序访问
async function main() {
const items = []; // Fill your items
const articles = []; // Fill your articles
await Promise.mapseries( items, async (item, i) => {
const itemx = await
Promise.all([w.getFileByServerRelativeUrl(item.FieldValuesAsText.FileRef).getItem()]);
console.log(item);
var likes
likes = await Promise.all(itemx.getLikedByInformation());
console.log("like " + likes.likeCount);
const comments = await Promise.all(itemx.comments.get());
console.log("Comments Count " + comments.length);
//articles[i] = articles[i]+likes+comments
articles[i].likecount = likes.likeCount
articles[i].commentcount = comments.length
articles[i].FileRef = item.FieldValuesAsText.FileRef
console.log(articles[i])
newst.push(articles[i++])
console.log(newst)
j++
})
`但它们不符合顺序`意思是??你说的是什么顺序?i和j的范围是什么。它们是什么。这个变量
i
来自哪里?为什么要递增j
?@panepeter我已经更正了,你现在能找到解决方案吗?@user8535404我知道你已经有了答案。不过,谢谢你清理了你的代码!它现在看起来更容易阅读和理解…这对试图提供帮助的人来说更具吸引力:)我正在使用react,我尝试了你的代码,但得到了这个错误=>“属性'mapseries'不存在于'typeof Promise'类型上”Promise.mapseries
是(或其他LIB的一部分,谁知道呢…),但不属于任何ES标准的一部分。如果您引用库中的函数,请这样说,否则测试代码的人会出错。