0

I'm trying to retrieve a list of commits within a repository using the Github API. After retrieving this list, I want to assign it to a list variable that I defined.

The reason why I want to do this is so I can extract information about the pull request, file(s) the commit is associated with, and the number of lines added/deleted from the file(s). I have the following code so far:

var listOfCommits = [];
const axios = require('axios');

axios.get(githubAPI).then(function (response) {
    listOfCommits = response['data'];
    console.log("in then part");
    console.log(listOfCommits[0]);
}).catch(function (error) {
    console.log("Error: something wrong happened");
}).finally(function () {
    console.log("hopefully this is done right...");
});

console.log("outside of the axios method");
console.log(listOfCommits[0]);
console.log("last debug");

I'm using Webstorm, so when I run the file using "node file.js" I get the following output:

outside of the axios method
undefined
last debug
in then part
[stuff retrieved from the repository]
hopefully this is done right...

The variable githubAPI is a string that stores the URL needed for the API to retrieve the necessary information. The goal of this code is to assign the information retrieved to the variables listOfCommits. Inside the axios.get() method, listOfCommits is defined, but when I try printing it out again after the method, it's undefined.

I know that it has something to do with the fact that axios.get() returns a promise, but I'm not sure how to fix this as I'm not too familiar with Javascript. Any help would be much appreciated. Thanks!

Roamer-1888
  • 19,138
  • 5
  • 33
  • 44
  • It makes sense you're getting undefined. you're calling the data from outside the promise, you have no guarantee that the data will be ready at that time. – Kevin.a Nov 20 '19 at 21:56
  • I know. But my question is, is there any way to make sure listOfCommits has the data outside of the promise so I can work with it? Or do I have to do everything within that promise, like getting the pull number and everything else I want to do? –  Nov 20 '19 at 22:11
  • Possible duplicate of [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Roamer-1888 Nov 20 '19 at 22:45

1 Answers1

0
let listOfCommits = null;
(() => {
  return new Promise((resolve, reject) => {
    axios.get('')
    .then((resp) => {
        resolve(resp['data']);
      },
      error => {
        reject(error);
      }
    );
})
})()
.then((data)=> {
  listOfCommits = data;      
})
luQ
  • 519
  • 1
  • 10
  • 25
  • Would I have to work with listOfCommits after .then((data)=> { listOfCommits = data; Because I tried this and if I put a print statement console.log(listOfCommits[0]); outside of this entire function, it still says it's undefined! –  Nov 20 '19 at 23:05
  • you should ask yourself whats going to happen with that listOfCommits... you might want to consider a 'more reactive' approach for your application. – luQ Nov 20 '19 at 23:30
  • The thing is that the axios call will be excecuted within another thread and won't be finished before hitting your console statement. – luQ Nov 20 '19 at 23:36