Skip to content

Latest commit

 

History

History

node-asynchronous

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 

Introduction to Node Asynchronous

Goal

  • Start to playing with asynchronous calls
  • Learn Promises

Mini katas

1. Hello world asynchronous with callback

  • Write a function that execute a callback after 1 second.
  • To validate the usage use a simple callback that says "Hello World!"

Tip: remember setTimeout

2. Hello world asynchronous with promises

  • Write a program using promises that say "Hello world!" after 1 second, when the promise is resolved.

Tips:

  • remember resolve
  • remember then

3. Failing Hello world asynchronous

  • Write a program using promises that say "The end of the world!" after 1 second, when the promise is rejected.

Tips:

  • remember reject
  • remember catch

4. Play with fs node module in asynchronous way

Use fs to read rates.json file.

  • Create a node app that returns the current change from EURO to a currency introduced as argument
  • Raise an Error when we do not have exchange rate for the introduced currency and show a message saying "Sorry, we don't support the currency XXX"

Tip: Investigate fs.readFile and do not use fs.readFileSync

5. Async / await

  • Transform the following code to use async / await instead of promises
function delayedString() {
    return new Promise((resolve, reject) => {
        setTimeout(() => resolve('Hello world'), 1000);
    });
}

function logDelayed() {
    delayedString()
    .then((message) => console.log(message));
}

logDelayed();

Tip: Documentation of async / await

6. HTTP request

Install request-promise to make http requests receiving a promise.

  • Create a program that receives an URL as argument, retrieves their content and shows the first 200 characters on the console.

7. Asynchronous test

Write a test to ensure the following function returns the string Hello world after a second

function delayedString() {
    return new Promise((resolve, reject) => {
        setTimeout(() => resolve('Hello world'), 1000);
    });
}

Tip: Remember to install mocha and chai.

Author

Luis Rovirosa