Building with Async Node.js

A series of exercises and modules to hone your asynchronous programming skills.

Scroll down...




This assignment is going to get you working directly with the concepts we've covered in this section. First, you will warm up with building promise-based interfaces. Then you will use promises to write promise based versions of common Node.js fs module functions. Finally, you will create your own implementation of the Node.js EventEmitter and implement it such that it can be switched out with the core Node.js version!

Getting Started

  1. Fork and clone the project's Github Repo.
  2. cd into the project directory
  3. Now we're ready to get started!


These few warmups will get you started working with promises.

  1. Create a promise that resolves the message "Hello Promise!" after 1 second

    • Use .then to resolve the promise and console.log the message
    p.then(function(message) {
      // 1 second delay
      console.log(message); //=> Hello Promise!
  2. Create a function with the following signature delay(milliseconds)

    • Your delay function should return a promise that resolves the value milliseconds after delaying for the specified number of milliseconds
    • Create a countDown function that uses the delay function such that the following chaining and output is possible:
      .then(countDown) //=> 1000
      .then(countDown) //=> 900
      .then(countDown) //=> 800
      .then(countDown) //=> 700
      .then(countDown) //=> 600
      .then(countDown) //=> 500
      .then(countDown) //=> 400
      .then(countDown) //=> 300
      .then(countDown) //=> 200
      .then(countDown) //=> 100
      .then(countDown); //=> Done!
  3. Create a function that accepts a number and returns a promise that resolves that number squared

    • The promise should reject if it is not passed a number
    • Now map an array of integers 1 to 9 to an array of promises using the function above
    • Use Promise.all to get the result of all of the promises in the array
  4. Create a function with this signature doBadThing(forRealz)

    • Return a promise that resolves to "Yay!" when forRealz is falsy
    • The promise should reject when forRealz is truthy
    • Now call doBadThing with both a true and false value chaining on .then and .catch
    • Experiment with using .catch vs supplying a reject handler in your .then call
    • Experiment using now try throwing an error in the resolve handler of your .then call
    • What do you notice about when the .catch handler and the reject handler are invoked?
  5. Commit!

File Operations

This part of the assignment takes the fs module in Node.js and has you wrap it with a promise based interface. Read through the docs of the following methods and try them out with a dummy text file:

  1. Create a fsp module that wraps these fs methods and makes the following promise based versions possible:

      .then(function(data) {
        // Outputs the file data
      .catch(function(err) {
    fsp.writeFile('./data/test.txt', 'Hello!')
      .then(function(res) {
        // Outputs the file data
        // after writing
      .catch(function(err) {
    fsp.appendFile('./data/test.txt', 'Hello again!')
      .then(function(res) {
        // Outputs the file data
        // after appending
      .catch(function(err) {
  2. Commit!

Create an Event Emitter from Scratch

Now on to the main... event. Your task here is to implement yours on a smaller version of the native Node.js EventEmitter. You will need to create a module that can perform the following operations:

  1. The returned value when requiring the module is a constructor that can be used as follows:

    var Emitter = require('./path/to/your/module');
    var emitter = new Emitter();
  2. Attach an event listener with emitter.on(eventType, callback)

  3. Attach subsequent listeners with emitter.on

  4. Emit an event with emitter.emit(eventType)

    • This should result in all listeners attached to that event being invoked
  5. Remove a listener with emitter.removeListener(eventType, callback)

  6. Remove all listeners on an event with emitter.removeAllListeners(eventType)

  7. Once you're satisfied that your emitter works the way it should commit!

Swap Your Event Emitter with the Native Node.js Event Emitter

Now require the event emitter from Node.js with:

var Emitter = require('events');

If you can replace your emitter with the native emitter you've successfully implemented the functionality of Node.js event emitters!

Finishing Up

  1. Commit, push, and submit a pull request back to the main repo
Pull request 300 Octocat 300

Sign up to track your progress for free

There are ( ) additional resources for this lesson. Check them out!

There are no additional resources for this lesson just yet!

Sorry, comments aren't active just yet!

Next Lesson: Introduction to Web Programming in Node