Diferència entre revisions de la pàgina «Promises»

De Wiket
Salta a la navegació Salta a la cerca
Línia 5: Línia 5:
 
<ol>
 
<ol>
 
<li>
 
<li>
Create a form with 3 fields at the least
+
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.
 
<!-- function validateForm(formData) {
 
<!-- function validateForm(formData) {
 
   return new Promise(function(resolve, reject) {
 
   return new Promise(function(resolve, reject) {

Revisió del 15:18, 14 març 2023

volver a M06 Desarrollo web en entorno cliente

Resources

Javascript Promises

Exercises

  1. 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.
  2. 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
  3. 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();
      });
    };