2017-10-06 3 views

답변

1

저장소 ID 당 1 요청을하는 개념 증명이 있습니다 (rate limiting 참조).

function getRepositories(ids) { 
    var promises = ids.map(id => getRepository(id).catch(() => {})); 
    return Promise.all(promises).then(repos => repos.filter(repo => repo !== undefined)); 

    function getRepository(id) { 
     return new Promise((resolve, reject) => { 
      const url = "https://api.github.com/repositories/" + id; 
      const xhr = new XMLHttpRequest(); 
      xhr.open("GET", url); 
      xhr.onreadystatechange =() => { 
       if (xhr.readyState == 4) { 
        if (xhr.status == 200) { 
         resolve(JSON.parse(xhr.response)); 
        } else { 
         reject(); 
        } 
       } 
      }; 
      xhr.send(); 
     }); 
    } 
} 

사용법 :

const githubRepoIds = [ 1,2,3 ]; 
getRepositories(githubRepoIds).then(repos => console.log(repos));