0

I am stuck in a js function.

function test() {
    setTimeout(function() {
        console.log('hrllo')
        return "I love You !!";
    }, 3000);
}

function main() {
    var result = test();
    console.log(result);     // must be I love You !!
}

I am working in pure Js which is JS 2017 supported means we can use async, await or promise. Can anybody please help me achieve the result?

SHIVAM SHARMA
  • 364
  • 3
  • 14
  • 1
    You _don't_ have async/await, or any promises. Maybe _trying that_ would be a good start? – jonrsharpe Feb 20 '22 at 18:04
  • I’d do `const test = () => new Promise((resolve) => setTimeout(resolve, 3000, "I love You !!")), main = async () => console.log(await test());`. – Sebastian Simon Feb 20 '22 at 18:06
  • @SebastianSimon I am receiving undefined. function test() { new Promise((resolve) => setTimeout(resolve, 3000, "I love You !!")) } async function main() { console.log(await test()); // undefined } – SHIVAM SHARMA Feb 20 '22 at 18:12
  • Receiving where? Please [edit] your post and include a [mre]. – Sebastian Simon Feb 20 '22 at 18:14

0 Answers0