These free mini-courses will give you a strong foundation in web development. Track your progress and access advanced courses on HTML/CSS, Ruby and JavaScript for free inside our student portal.
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!
cd
into the project directoryThese few warmups will get you started working with promises.
Create a promise that resolves the message "Hello Promise!"
after 1 second
.then
to resolve the promise and console.log
the messagep.then(function(message) {
// 1 second delay
console.log(message); //=> Hello Promise!
});
Create a function with the following signature delay(milliseconds)
delay
function should return a promise that resolves the value milliseconds
after delaying for the specified number of millisecondscountDown
function that uses the delay
function such that the following chaining and output is possible:delay(1000)
.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!
Create a function that accepts a number and returns a promise that resolves that number squared
Promise.all
to get the result of all of the promises in the arrayCreate a function with this signature doBadThing(forRealz)
"Yay!"
when forRealz
is falsyforRealz
is truthydoBadThing
with both a true
and false
value chaining on .then
and .catch
.catch
vs supplying a reject
handler in your .then
call.then
call.catch
handler and the reject
handler are invoked?Commit!
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:
Create a fsp
module that wraps these fs
methods and makes the following promise based versions possible:
fsp.readFile('./data/lorem.txt')
.then(function(data) {
// Outputs the file data
console.log(data);
})
.catch(function(err) {
console.error(err);
});
fsp.writeFile('./data/test.txt', 'Hello!')
.then(function(res) {
// Outputs the file data
// after writing
console.log(res);
})
.catch(function(err) {
console.error(err);
});
fsp.appendFile('./data/test.txt', 'Hello again!')
.then(function(res) {
// Outputs the file data
// after appending
console.log(res);
})
.catch(function(err) {
console.error(err);
});
Commit!
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:
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();
Attach an event listener with emitter.on(eventType, callback)
Attach subsequent listeners with emitter.on
Emit an event with emitter.emit(eventType)
Remove a listener with emitter.removeListener(eventType, callback)
Remove all listeners on an event with emitter.removeAllListeners(eventType)
Once you're satisfied that your emitter works the way it should commit!
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!