Skip to content

Latest commit

 

History

History
48 lines (37 loc) · 1.24 KB

js-async.md

File metadata and controls

48 lines (37 loc) · 1.24 KB

Asynchronous Functions

  1. Rewrite the following function using Promises:

    function add(a, b) {
      return a + b;
    }
  2. Rewrite the following function using Promises:

    function squareRoot(x) {
      if (x >= 0) {
        return Math.sqrt(x);
      } else {
        return { error: "'x' must be positive" };
      }
    }
  3. Using the News API, retrieve the associated photos of the top headlines. Use axios to do all requests:

    • First register on the News API website to get an API key (it's easy).

    • Write a script to retrieve the JSON information about the top headlines for Spain.

    • Download the image of a single news article (taken form the urlToImage field) using code like this:

      axios({
        method: "get",
        url: "https://someurl.com/some-file.jpg",
        responseType: "stream",
      }).then(function (response) {
        response.data.pipe(fs.createWriteStream("/my/dir/downloaded.jpg"));
      });

      The fs package is internal to NodeJS.

    • Create an array of Promises to retrieve all photos at in parallel, using Promise.all.