0

The code works as expected:

var a = [1,2,3,4,5,6,7];
async function showArray(arr){
  for (var i = 0; i<arr.length; i++) {
    await showElement(arr[i]);
  }
}
async function showElement(x){
  console.log(x);
  document.getElementById('result').value+=x.toString();
  return new Promise((resolve) => {
    setTimeout(function () {
      resolve("");
    }, 1000);
  });
}
(async()=>{ await showArray(a)})();
<textarea id="result"></textarea>

When I enter the code into my console directly, it says:

There is a Promise <pending>, indicating there is a unresolved promise.

Is this a problem that I need to fix and how can I fix this?

AGamePlayer
  • 7,404
  • 19
  • 62
  • 119
  • 2
    Not a problem, it just means that your async function (which is immediately invoked) returns a Promise... as expected – CertainPerformance Sep 08 '22 at 04:39
  • also can use promise.all – TimoDevs Sep 08 '22 at 05:25
  • `var a = [1,2,3,4,5,6,7]; function showArray(arr){ return arr.map(() => showElement()); } function showElement(){ return new Promise((resolve) => { setTimeout(resolve, 1000); }); } (async () => console.log(await Promise.all(showArray(a))))()` – TimoDevs Sep 08 '22 at 05:26

0 Answers0