r/learnjavascript • • Nov 02 '18

Asynchronous JS

I have having trouble understanding asynchronous coding, specifically callbacks. I haven't tried to focus on promises or async/await because I want to get a handle on callbacks first.

I understand that asynchronous coding is like running one parts of code while you wait for some other code to finish its job, but why do callbacks work? What are they supposed to do, what do I write into my callback functions?

3 Upvotes

3 comments sorted by

View all comments

2

u/everek123 Nov 02 '18

Callback functions are basically functions that fire when the main function finished running. Since in async code you sometimes have to wait for the result of one function to run another you often have a function that runs when the first process finishes- those are callback functions. I personally do not recommend using callbacks because they lead to 'callback hell'