You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: _posts/2014-2-17-javascript-gotchas.md
+2-2Lines changed: 2 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,11 +1,11 @@
1
1
---
2
2
layout: post
3
-
title: "JavaScript gotchas"
3
+
title: "JavaScript must-knows"
4
4
author: NodeKC
5
5
tags:
6
6
---
7
7
8
-
# JavaScript gotchas
8
+
# JavaScript must-knows
9
9
10
10
JavaScript is a language that many developers are familiar with but lack a deep understanding. The language lends itself to new programmers, in part, because of its simple syntax. Many fundamental concepts and features of the language can be overlooked with a simple procedural approach. The goal of this lab is to give you a better understanding of some core concepts and important language features to understand about JavaScript.
Copy file name to clipboardExpand all lines: _posts/2014-2-27-streams.md
+15-8Lines changed: 15 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,10 +1,17 @@
1
+
---
2
+
layout: post
3
+
title: "Streams"
4
+
author: NodeKC
5
+
tags:
6
+
---
7
+
1
8
# Streams
2
9
3
-
Streams are one of the most amazing features of Node. However, it's not a new thing. \*nix systems use I/O streams to move data from one process to another.
10
+
Streams are one of the most amazing features of Node. However, it\'s not a new thing. \*nix systems use I/O streams to move data from one process to another.
4
11
5
12
## Read Streams
6
13
7
-
The first stream we'll discuss is a read stream. As you might expect this stream supports only reading. Let's take a look at an example.
14
+
The first stream we will discuss is a read stream. As you might expect this stream supports only reading. Let\'s take a look at an example.
8
15
9
16
Start by creating a file called ```example.txt``` and add whatever contents you like. Create another file called `readable.js` and add the following:
10
17
@@ -61,9 +68,9 @@ rs.on('end', function () {
61
68
{% endhighlight %}
62
69
63
70
64
-
We get to learn a few different things in this example. First, we create a new readable stream. The `MyStream` function is inheriting the prototype from Node core's `ReadableStream` and in its constructor calls the `ReadableStream` constructor. We must call the constructor of the core implementation in order to perform the necessary initialization of our stream. We are required to implement the internal method `_read` which is called on every read request. In this example the first read request is issued whenever we subscribe to the `'data'` event. There are other ways to do this which we'll cover next.
71
+
We get to learn a few different things in this example. First, we create a new readable stream. The `MyStream` function is inheriting the prototype from Node core's `ReadableStream` and in its constructor calls the `ReadableStream` constructor. We must call the constructor of the core implementation in order to perform the necessary initialization of our stream. We are required to implement the internal method `_read` which is called on every read request. In this example the first read request is issued whenever we subscribe to the `'data'` event. There are other ways to do this which we will cover next.
65
72
66
-
You'll notice in this example that we are able to control the flow of data from a readable stream through `pause` and `resume`. This is incredibly important to allow your application to keep up with incoming data and only receive data when needed.
73
+
You will notice in this example that we are able to control the flow of data from a readable stream through `pause` and `resume`. This is incredibly important to allow your application to keep up with incoming data and only receive data when needed.
67
74
68
75
The above example reads data by the buffer. However, we have more fine grained control over how much data we receive through the `read` method. The next example will give you an idea of the series of events that occurs when reading from a stream that can provide data at various times. This is exactly the situation when dealing with network connections. Feel free to copy and paste this snippet. Study the output and understand the chain of events before moving on.
69
76
@@ -126,11 +133,11 @@ rs.on('readable', function () {
126
133
log('Reached end of program. It\'s now alive because of async callbacks pending.');
127
134
{% endhighlight %}
128
135
129
-
We've successfully read 2 bytes from our underlying stream with each iteration of the loop. Despite the number of calls to `read` we only invoke our internal function when new data is actually needed.
136
+
We\'ve successfully read 2 bytes from our underlying stream with each iteration of the loop. Despite the number of calls to `read` we only invoke our internal function when new data is actually needed.
130
137
131
138
## Write Stream
132
139
133
-
An example of where write streams are used are HTTP responses or standard output. Just like `ReadableStream`s, `WritableStream`s have a built in mechanism for rate limiting. These streams are aware of when the underlying buffer has been flushed and provide the caller with this information. This is very useful so that you don't seen more than a client can handle. If the client isn't able to accept information fast enough it will buffer in memory. Let's have a look at a simple example of a commonly used write stream. Open a node REPL and input the following:
140
+
An example of where write streams are used are HTTP responses or standard output. Just like `ReadableStream`s, `WritableStream`s have a built in mechanism for rate limiting. These streams are aware of when the underlying buffer has been flushed and provide the caller with this information. This is very useful so that you don\'t seen more than a client can handle. If the client isn\'t able to accept information fast enough it will buffer in memory. Let\'s have a look at a simple example of a commonly used write stream. Open a node REPL and input the following:
Much praise is given to node by its community for providing an extremely simple interface for chaining streams together using `pipe`. All \*nix developers are familar with having the ability to pipe the standard output of one process into the standard input of another using a `|`. Let's see how we could take our standard input and direct it to our processes output stream.
179
+
Much praise is given to node by its community for providing an extremely simple interface for chaining streams together using `pipe`. All \*nix developers are familar with having the ability to pipe the standard output of one process into the standard input of another using a `|`. Let\'s see how we could take our standard input and direct it to our processes output stream.
173
180
174
181
{% highlight javascript %}
175
182
process.stdin.pipe(process.stdout);
@@ -181,7 +188,7 @@ We can test this by placing the above contents in a file called pipe.js and runn
181
188
echo 'This is going to be piped out' | node pipe.js
182
189
{% endhighlight %}
183
190
184
-
How easy was that! If we didn't have `pipe` here's how we'd accomplish the same task. You can see pipe is taking care of a lot for us. What you do not see in this example is that `pipe` is automatically handling backpressure.
191
+
How easy was that! If we didn\'t have `pipe` here's how we\'d accomplish the same task. You can see pipe is taking care of a lot for us. What you do not see in this example is that `pipe` is automatically handling backpressure.
0 commit comments