简体   繁体   English

如何在映射函数中使用 async 和 await 进行顺序执行

[英]How to use async and await inside a map function for sequential executing

Hi for me async and await is quiet new .I have function call which requires await method so as a result i need to use async and await.嗨对我来说 async 和 await 是安静的新。我有需要 await 方法的函数调用,因此我需要使用 async 和 await。 I'm getting the values but they are not in order.我得到了这些值,但它们不是按顺序排列的。

Below is my async and await map function下面是我的异步和等待地图功能

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++])
  })

Any suggestion will really be helpful任何建议都会很有帮助

You have to await promises of the map function to get results in order.您必须等待 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);
}

Each map item function runs asynchronously so there is no guarantee of order inside the map function, but you can return value from the map function which can be resolved to an ordered array by using await Promise.all() since the promise array returned by the map function is in the correct order.每个映射项函数都是异步运行的,因此无法保证映射函数内部的顺序,但是您可以从映射函数返回值,该值可以通过使用await Promise.all()解析为有序数组,因为由map 函数的顺序正确。

try Promise.mapSeries for Sequential access with async await.尝试使用 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++
  })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM