-2

In my code i have an async function that retuns an object i have to work with it outside the function but i dont really know how to assign it to a variable, I know this post might be a duplicate but i read the others and tried and it doesn't work.

async function name(url) {
//function

return {
    item1:{ ....},
    item2:{....}
} 
}

let p = name(url); 
console.log(p);

it returns in the console:

Promise { <pending> }

but it doesn't log the output

How can i fix that?

1 Answers1

1

Promises are async. You need to get the result using

  1. A callback, specified to the then method of the promise.
name(url).then(p => console.log(p))
  1. Or use an async context and use the await keyword:

(async(){

let p = await name(url);
console.log(p);

})();

EliSherer
  • 1,597
  • 1
  • 17
  • 29