...
/Solution Review: Asynchronous Callback Functions
Solution Review: Asynchronous Callback Functions
This lesson will explain the solution to the problem in the previous lesson.
We'll cover the following...
Solution #
Press + to interact
const getTodo = callback => {setTimeout(() => {callback ({ text: 'Complete Code Example' })}, 2000)}function display(){getTodo(todo => {console.log(todo.text)})}display()
Explanation #
Before we discuss the solution above, let’s discuss what the original code was doing.
Press + to interact
const getTodo = () => {setTimeout(() => {return { text: 'Complete Code Example' }}, 2000)}function display(){const todo = getTodo()console.log(todo.text)}display()
The getTodo
function has the setTimeout
function which returns the object {text : 'Complete Code Example'}
after two seconds pass.
In the display
function, we call the getTodo
function and save the result in the variable todo
. Finally, we display the ...