Async Await
La revisió el 15:55, 14 març 2023 per Lzabala (discussió | contribucions) (Es crea la pàgina amb «=Exercises= <ol> <li> Create a form with 2 fields a user name and a email. Create a function (with promises) to validate the form and submit the form if it is correct....».)
Exercises
- Create a form with 2 fields a user name and a email. Create a function (with promises) to validate the form and submit the form if it is correct.
- Make a web page with a button (button text: Load image), when we click on that button you have use "promises" to load an image from your server and catch the errors, if there are
- With the given code, create a web page that call a server with some data (it's doesn't matter what data) and print the data on the page applying some kind of format.
const loadJSON = (file) => { return new Promise((resolve, reject) => { const xhr = new XMLHttpRequest(); xhr.open('GET', file, true); xhr.responseType = 'json'; xhr.onload = () => { if (xhr.status === 200) { resolve(xhr.response); } else { reject(xhr.statusText); } }; xhr.onerror = () => reject(xhr.statusText); xhr.send(); }); };
-
Using the given code and this api ( https://restcountries.com/ ), create a web application that asks user to enter a capital form some country and displays the name of the country and its flag
const findData = async () => { const res = await fetch("API URL"); const country = await res.json(); }; findData(); findData = new Promise(function (myResolve,myReject) { res = fetch("API URL") if(res){ myResolve(res) } }); findData.then( function (value) { //console.log(value.json()) return value.json() } ).then(res =>{ console.log(res[0].name.common) });
- Modify the previous exercise to display in addition to the name of the country and its flag, the name and flag of the neighboring countries. Hint: Promises.all()