0
let result;

function HelloFunction(callbackfn){
    setTimeout(() => result = 50 ,2000);
    callbackfn(result);
};

HelloFunction((a) => console.log(`Result is ${a}`));

Output: Result is undefined. Why not Result is 50 as it's a callback

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
AjithKumar
  • 21
  • 6
  • It's because you invoke `callbackfn()` ***before*** you set the value of `result`. Remember, `result` is only set after the 2sec delay, yet the callback is called immediately. You need to change your logic to put the callback in the timeout, after setting the value of `result`: https://jsfiddle.net/RoryMcCrossan/4svdmbho/ – Rory McCrossan Jan 22 '20 at 16:32
  • 1
    Thank you @RoryMcCrossan – AjithKumar Jan 22 '20 at 16:41

0 Answers0