Promises

De Wiket
Salta a la navegació Salta a la cerca

volver a M06 Desarrollo web en entorno cliente

Resources

Javascript Promises

Exercises

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