0

// getData() function is returning null right now , but Actually I want to return a value after 1.5 seconds

showData = (data) => {
        console.log("showing" , data)
    }
    getData = async (a,b) => {
        let x = null;
        await setTimeout(() => {
            x = a*b;
            console.log(x) //200
        }, 1500);
        console.log(x); //null --should run after timeout
        return x;
    }
    getData(10,20);

1 Answers1

-2

This will do it

async function getData (a, b) {
  return new Promise(resolve =>
    setTimeout(() => {
      const x = a * b;
      resolve(x)
    }, 1500)
  )
}

getData(10, 20).then(result => console.log(result));
ProfDFrancis
  • 8,816
  • 1
  • 17
  • 26