Multiple Independent Promises in Then Function

I think this is a scenario where it might be useful to use async/await instead

const func1 = () => {
  return new Promise(resolve => {
    setTimeout(() => {
      resolve(["item1", "item2", "item3"]);
    }, 2000);
  });
};

const func2 = item => {
  return new Promise(resolve => {
    console.log('item', item)
    setTimeout(() => {
      resolve(`${item} : func2`);
    }, 2000);
  });
};

const func3 = item => {
  return new Promise(resolve => {
    setTimeout(() => {
      resolve(`${item} : func3`);
    }, 2000);
  });
};

const exampleAsyncFunction = async () => {
  const result1 = await func1;
  const result2 = await func2(result1);
  const result3 = await func3(result1);
  return [result2, result3];  
  //I'm just choosing to return an array with the 2 results, 
  //but you can return whatever you like.
}

exampleAsyncFunction()
  .then((finalResult) => {
    console.log('finalResult:', finalResult)
    const [finalResult2, finalResult3] = finalResult; //deconstructing array
    console.log('2:', finalResult2)
    console.log('3:', finalResult3)
  })
/r/learnjavascript Thread