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);
})
}
EDIT: Yes, if you need a promise to resolve, you need to await
it, requiring an async function
.
The "old" way of using Promises (which await
is actually just syntactic sugar for) does not require you to make the function async
, but executes non-linearly. Here's an example:
// I've cleaned up the code a bit, removing this and var
async function javed() {
// the function is async, so it always returns a Promise. No await necessary.
return createMap();
}
function createMap() {
var map2 = new Map();
map2.set('11', true);
map2.set('22', true);
map2.set('33', false);
map2.set('44', true);
return map2;
}
function Dome(res) {
res.forEach(x => {
console.log('inside dome', x);
})
}
console.log("before javed");
// call javed, then Dome without a top level async function but the then method of a Promise:
// less verbose than javed().then(function(resolvedMap){Dome(resolvedMap)});
javed().then(Dome);
console.log("this will be executed before the callback function Dome!");