Skip to main content
fix typo
Source Link
bolav
  • 7k
  • 2
  • 22
  • 43

sleep is blocking, and not returning controllcontrol to the event loop, so your code just sleeps right where it says sleep.

If you convert it to async, with setTimeout like this:

function getData(key){ console.log("IN FUNCTION " + key); setTimeout(function() { var data = "got data for " + key; things.keep(key, data); console.log("DONE WIH FUNCTION " + key); }, 5000); } 

I get this output:

start IN FUNCTION one IN FUNCTION two end DONE WIH FUNCTION one got data for one got data for two DONE WIH FUNCTION two 

Which looks correct to me.

sleep is blocking, and not returning controll to the event loop, so your code just sleeps right where it says sleep.

If you convert it to async, with setTimeout like this:

function getData(key){ console.log("IN FUNCTION " + key); setTimeout(function() { var data = "got data for " + key; things.keep(key, data); console.log("DONE WIH FUNCTION " + key); }, 5000); } 

I get this output:

start IN FUNCTION one IN FUNCTION two end DONE WIH FUNCTION one got data for one got data for two DONE WIH FUNCTION two 

Which looks correct to me.

sleep is blocking, and not returning control to the event loop, so your code just sleeps right where it says sleep.

If you convert it to async, with setTimeout like this:

function getData(key){ console.log("IN FUNCTION " + key); setTimeout(function() { var data = "got data for " + key; things.keep(key, data); console.log("DONE WIH FUNCTION " + key); }, 5000); } 

I get this output:

start IN FUNCTION one IN FUNCTION two end DONE WIH FUNCTION one got data for one got data for two DONE WIH FUNCTION two 

Which looks correct to me.

Source Link
bolav
  • 7k
  • 2
  • 22
  • 43

sleep is blocking, and not returning controll to the event loop, so your code just sleeps right where it says sleep.

If you convert it to async, with setTimeout like this:

function getData(key){ console.log("IN FUNCTION " + key); setTimeout(function() { var data = "got data for " + key; things.keep(key, data); console.log("DONE WIH FUNCTION " + key); }, 5000); } 

I get this output:

start IN FUNCTION one IN FUNCTION two end DONE WIH FUNCTION one got data for one got data for two DONE WIH FUNCTION two 

Which looks correct to me.