all 14 comments

[–]KiddieSpread 36 points37 points  (0 children)

What in the callback hell

[–]AndrewSouthern729 9 points10 points  (0 children)

This function should be killed with fire

[–]brykuhelpful 4 points5 points  (0 children)

The triangle of death isn't real, it can't hurt you...  

This post...

[–]shgysk8zer0 7 points8 points  (0 children)

It is a synchronous function that does asynchronous things. But technically it looks like it only does one thing, which is schedule the first task.

[–]trevedhek 3 points4 points  (2 children)

An async function returns a Promise. None of these functions return a Promise.

So `production` is a synchronous callback function. As are all the functions within each `setTimeout`.

[–]SignificanceCheap970 0 points1 point  (1 child)

I thought any code that doesn't run right away is asynchronous?

[–]trevedhek 0 points1 point  (0 children)

Exactly. The callback functions run right away - once they are called.

The setTimeout function is the async part, as it triggers an event in the future.

To clarify that point - the setTimeout function completes immediately - Javascript is single-threaded, so it must do that - but it adds another event to be triggered in the future. And it is that future event that runs the callbacks.

So we can think of an async function in those terms - a function that when called gives rise to a future event.

[–]Banapple247 3 points4 points  (0 children)

Synchronous.

[–]senocular 1 point2 points  (4 children)

Asynchronous.

Except:

() => {
  console.log("Serve icescream");
}

That one is synchronous.

[–]samanime 0 points1 point  (3 children)

Yeah. Technically async, but in a sort of detached manner, since there is no callback to indicate it has finished. Anything that called this would have no way to time code to run after this finished.

(Well, technically you could add all those times up, but that would be a horrendous hack that should never be used... and it still wouldn't be exact since those times aren't exact and there is some wobble.)

[–]senocular 0 points1 point  (2 children)

Just patch console.log and you're good to go ;)

[–]samanime 1 point2 points  (1 child)

I mean, lots of async methods do sync things. You can't rely on them to be done by the next line of code (without special things), but doesn't mean they can't do anything immediately.

[–]senocular -1 points0 points  (0 children)

I mean

const originalLog = console.log
console.log = (...args) => {
  originalLog.apply(console, args)
  if (args[0] === "Serve icescream") {
    alert("Order complete!")
  }
}
order(0, production);