0

I came across one of the problem with Async and await. Where it is returning Promise array instead of actual Array of values. I understand async await uses Promise and generators internally but wanted to understand how i can solve the below issue.

let object = [
    "hello1",
    "hello2",
    "hello3",
    "hello4"
]

async function funct(value){
    return new Promise((res,rej)=> res( value + '-world'));
}



let resultArray = object.map(async (e)=>{
    let result = await funct(e);
    return result
})


console.log(Promise.all(resultArray).then((r)=> console.log(r)));

output is :

Promise { <pending> }
[ 'hello1-world', 'hello2-world', 'hello3-world', 'hello4-world' ]

But I am looking for actual array of values.

[ 'hello1-world', 'hello2-world', 'hello3-world', 'hello4-world' ]

Can you please suggest on this.?

wxker
  • 1,841
  • 1
  • 6
  • 16
monk_7
  • 17
  • 2
  • `async` functions [return a Promise by definition](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function) - this is not a problem in the code, it's a problem in your understanding of `async` functions – Jaromanda X Jun 19 '21 at 06:03
  • Remove the `console.log` around the `Promise.all`, that’s what’s logging the additional promise… – deceze Jun 19 '21 at 06:04
  • Thanks! @deceze and Jaromanda. I am new to Promise and this really helps . – monk_7 Jun 19 '21 at 06:53

0 Answers0