Browse files

Initial talk README

  • Loading branch information...
Jeffrey Horn
Jeffrey Horn committed Mar 7, 2016
1 parent 5c571f5 commit 96fa7c608c37c3b48898ce2c7b621322a964fa87
Showing with 103 additions and 41 deletions.
  1. +103 −41
@@ -1,64 +1,126 @@
[![General Assembly Logo](](

# Introduction to the Node API
# Using Promises with the Node API

## Prerequisites

- Familiarity with JavaScript as a language.
- Familiarity with JavaScript in the browser environment.
- [ga-wdi-boston/node-api](

## Objectives

By the end of this, developers should be able to:

- Reference the Node API documentation for using JavaScript outside the
- Reference the MDN JavaScript documentation for JavaScript language features.
- Write a Node script using the File System API.
- Explain the value of using promises instead of callback interfaces.
- Read Node documentation that uses callbacks and translate that into
implementations using promises.
- Convert Node scripts using callbacks into scripts using promises.

## Preparation

1. [Fork and clone](
this repository.
1. Install dependencies with `npm install`.

## JavaScript, the Browser, and Node

- JavaScript: a language specified by ECMA and implemented independently
- Browser: an environment for running JavaScript (among other things)
- Node: an environment for running JavaScript outside the browser

How are the two environments similar? How do they differ?

## Lab: Research the File System API

Take a few minutes to read the following API documentation for the Node File
System module.

While you're reading, imagine how you'd use each function. Write some example
code in your notebook. Try to explain what each function does in your own words,
including what sorts of arguments it takes and each argument's type.

If you finish early, look for other interesting functions in the File System

1. [`fs.readdir(path, callback)`](
1. [`fs.readdirSync(path)`](
1. [`fs.readFile(file[, options], callback)`](
1. [`fs.writeFile(file, data[, options], callback)`](

## Read-Along: `copy-json.js`

## Code-Along: `hey-yall.js`

## Lab: Write a Randomizer
## Callbacks versus Promises

- Callbacks can be messy when they're nested: "callback hell". See [`lib/copy-json.js`](lib/copy-json.js).
- Each callback will have to handle it's own errors if necessary.
- In complex programs, it will be hard to tell in what order callbacks fire.

- Promises, like callbacks, make asynchronicity explict.
- Promises, unlike callbacks, have a predictable order of execution.
- Promises are easier to read than callbacks.
- Promises can simplify error handling.

## Lab: Research the Promises API

Take a few minutes to read the following API documentation for the [native

Note function signatures and argument types as you read. What kind of object
does a promise take when it is constructed?

1. [Promise Syntax](
1. [Promise.prototype](

## Read-Along: Using Promises Instead of Callbacks

// remember that callback is something you write, in this case to perform some
// processing on parsed JSON
let readJSON = function (filename, callback){
fs.readFile(filename, 'utf8', function (err, res){
if (err) return callback(err); // what's going on here?
callback(null, JSON.parse(res)); // what if JSON.parse errors out?

What are some weaknesses in this code? And the following?

let readJSON = function (filename, callback){ // 👀 here
fs.readFile(filename, 'utf8', function (err, res){
if (err) return callback(err); // pass the error from readFile
try {
res = JSON.parse(res);
} catch (ex) {
return callback(ex); // pass the error from JSON.parse
callback(null, res); // don't pass the error, since we should have caught it

What about this instead?

let readJSON = function (filename){ // 👀 here
return new Promise((resolve, reject) => {
fs.readFile(filename, 'utf8', (err, res) => {
if (err) {
} else {
}).then((res) => {
return JSON.parse(res)
}).catch((err) => {

That's too verbose. This is better:

let readJSON = function (filename){
return new Promise((resolve, reject) => {
fs.readFile(filename, 'utf8', (err, res) => {
if (err) {
} else {
}).then(JSON.parse).catch(console.log); // what can we surmise about .then?

## Code-Along: Promisify `copy-json.js`

## Lab: Pomisify `hey-yall.js`

## Lab: Promisify `randomizer.js`

## Additional Resources

- [Docs | Node.js](
- [JavaScript | MDN](
- [Node Courses from CodeSchool](
- [NodeSchool](
- [Promise - JavaScript | MDN](
- [Promises](
- [Promisees · Courtesy of](
- [wbinnssmith/awesome-promises: A curated list of useful resources for JavaScript Promises](
- [How to escape Promise Hell — Medium](

## [License](LICENSE)

0 comments on commit 96fa7c6

Please sign in to comment.