Once a stream is created, it typically starts immediately, unless it's paused. In this video, we'll start a stream and then pause it. Let's go to the Exercise Files, load chapter 9, video 3, and then Copy these two files to the desktop. Now let's have a look at control.js. Everything in this file is the same as the previous video, except for lines 5 through 12. First we're calling the pause method of this stream. Calling stream.pause right away, will pause the stream, before any event listener's have a chance to respond to an incoming chunk.
It's also possible to pause a stream after it's already started. Any time you pause a stream, it will stay paused until you resume it. On line 7, we're just sending a message to the console saying that we're paused. Then on line 9 we're calling setTimeout. We're passing in 1,000 milliseconds, which will come out to one second, and then we have a callback function. So in this example, we're waiting one second before resuming the stream. The stream starts back up again, when we call stream.resume.
Once a stream resumes, the event listeners will once again start receiving chunks. So let's run this file. Go to Terminal, and change directory to the desktop, capital D. And then type node control and press Return. So for a second there we saw the paused message, then after one second passed, we saw a resuming message and then all the even listeners kicked into action. Streams can be paused by calling the pause method.
They stay paused until you start them again using resume. If you don't want a stream to start automatically, call the pause method before any event listeners are attached. In the next video, we'll pipe data from a stream, rather than attaching event listeners.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 98890 Viewers
61 Video lessons · 86194 Viewers
71 Video lessons · 70032 Viewers
56 Video lessons · 102280 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Your file was successfully uploaded.