[ Web Proxy ]
URL:
Viewing: https://da.javascript.info/task/fetch-users [Back]  [Original]

Fetch users from GitHub
DA

Vi nsker at gre dette open source-projekt tilgngeligt for folk over hele verden.

Hjlp med at overstte indholdet af denne tutorial til dit sprog!

    Sg p Javascript.info:
    Sg i tutorialen:
    Lyst temaMrkt tema
    DanskEnglishEspaolFranaisIndonesiaItalianoTrkeOzbek
    tilbage til lektionen
    Dette materiale er kun tilgngeligt p flgende sprog: English, Espaol, , Franais, Indonesia, Italiano, , , , , Ozbek, . Hjlp os med at overstte til Dansk.

    Fetch users from GitHub

    Create an async function getUsers(names), that gets an array of GitHub logins, fetches the users from GitHub and returns an array of GitHub users.

    The GitHub url with user information for the given USERNAME is: https://api.github.com/users/USERNAME.

    Theres a test example in the sandbox.

    Important details:

    1. There should be one fetch request per user.
    2. Requests shouldnt wait for each other. So that the data arrives as soon as possible.
    3. If any request fails, or if theres no such user, the function should return null in the resulting array.

    bn en sandbox med tests.

    lsning

    To fetch a user we need: fetch('https://api.github.com/users/USERNAME').

    If the response has status 200, call .json() to read the JS object.

    Otherwise, if a fetch fails, or the response has non-200 status, we just return null in the resulting array.

    So heres the code:

    async function getUsers(names) {
      let jobs = [];
    
      for(let name of names) {
        let job = fetch(`https://api.github.com/users/${name}`).then(
          successResponse => {
            if (successResponse.status != 200) {
              return null;
            } else {
              return successResponse.json();
            }
          },
          failResponse => {
            return null;
          }
        );
        jobs.push(job);
      }
    
      let results = await Promise.all(jobs);
    
      return results;
    }

    Please note: .then call is attached directly to fetch, so that when we have the response, it doesnt wait for other fetches, but starts to read .json() immediately.

    If we used await Promise.all(names.map(name => fetch(...))), and call .json() on the results, then it would wait for all fetches to respond. By adding .json() directly to each fetch, we ensure that individual fetches start reading data as JSON without waiting for each other.

    Thats an example of how low-level Promise API can still be useful even if we mainly use async/await.

    bn lsningen med tests i en sandbox.


    Web Proxy Viewer  |  New URL  |  Original Page