1

// 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);
2

1 Answer 1

-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));

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.