1

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. 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

7
  • ` but they are not in order` means?? About which order you are talking about? Commented Nov 27, 2019 at 10:59
  • what is scope of i and j. And what are they. Commented Nov 27, 2019 at 11:05
  • Where is this variable i coming from and why are you incrementing j? Commented Nov 27, 2019 at 11:05
  • 1
    @panepeter I have corrected, can you please find the solution now? Commented Nov 27, 2019 at 11:42
  • 1
    @user8535404 I see you already have your answer. Still, thanks for cleaning your code up! The way it looks now is just so much easier to read and understand … that's a lot more attractive to people trying to help out :) Commented Nov 28, 2019 at 12:50

2 Answers 2

2

You have to await promises of the map function to get results in order.

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.

Sign up to request clarification or add additional context in comments.

Comments

-2

try Promise.mapSeries for Sequential access with async await.

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

2 Comments

I'm using react , I tried your code but getting this error => " Property 'mapseries' does not exist on type 'typeof Promise'"
Promise.mapSeries is part of the bluebird library (or other libs as well, who knows …), but not part of any ES standard. If you refer to functions from libraries, please say so, because otherwise people testing your code will get errors.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.