Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

Share this Page URL

Hour 4. Callbacks > Synchronous and Asynchronous Code - Pg. 50

50 HOUR 4: Callbacks TRY IT YOURSELF If you have downloaded the code examples for this book, this code is hour04/example04. The following steps demonstrate callbacks: 1. Copy the code in Listing 4.1 into a file on your system called app.js. 2. Create two files, called file1.txt and file2.txt, and add some text to them. You might want to use some Latin text from 3. Run the script: node app.js 4. You should see that the callbacks from reading the file are fired first, then the two http. get operations. 5. Run the script a few more times. Think about how these events are happening asynchro- nously and how callbacks are used to do other things when operations have finished. Synchronous and Asynchronous Code