Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Unable to iterate over dataset returned from an async function

I am a JavaScript beginner. have an async function, that has an await method in it, problem is that after obtaining the return value from that async function when I am passing it as a parameter to another function and iterating over it using a forEach it says data.ForEach is not a function

Here is my code

async function javed(){
  var map1 = new Map();

  map1 = await createMap();


  return map1;


 }

 function createMap(){

  var map2 = new Map();
  map2.set('11', true);
  map2.set('22', true);
  map2.set('33', false);
  map2.set('44', true);

  return map2;


 }

  let x = this.javed();
  this.Dome(x);



  function Dome(res){

    res.forEach(x =>{
      console.log('inside dome', x);

    })

  }

This shows the following error

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel


Uncaught TypeError: res.forEach is not a function
at Dome (myfile.js:83:9)

Also I am not getting during the iteration how would I get the individual key and value if I want to manipulate or just console them?

>Solution :

Your problem is, that all async functions return a Promise.
So you’re not calling forEach() on a Map as you would expect, but instead on a Promise which does not have that function.
Use let x = await javed(); in order to resolve that promise and get back a Map.

Generally I would advise you to use the this keyword sparingly in JS, you don’t need it at all in your example and it can often be something other that what you expect. Also, try not to use var anymore. const and let are much more isolated from external scripts and their scoping is easier to understand.

Here’s a working example with the added await. If top-level await is not supported, you can wrap the behavior in a new async function as I have done here:

async function javed() {
  var map1 = new Map();
  // createMap is not async, so no reason to await it
  map1 = await createMap();
  return map1;


}

function createMap() {
  var map2 = new Map();
  map2.set('11', true);
  map2.set('22', true);
  map2.set('33', false);
  map2.set('44', true);
  return map2;
}

async function doEverything() {
// you need to await javed
let x = await this.javed();
this.Dome(x);
}
// call the async function
doEverything();



function Dome(res) {

  res.forEach(x => {
    console.log('inside dome', x);

  })

}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading