- Start to playing with asynchronous calls
- Learn Promises
- 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
- Write a program using promises that say "Hello world!" after 1 second, when the promise is resolved.
Tips:
- remember
resolve
- remember
then
- 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
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
- 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
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.
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);
});
}