1

I've been working on a project and I need some help regarding this: I have a promise inside a function and I need to return the value of "current" so that I can use it elsewhere. I'm retrieving data from Firebase, then I shuffle the result and extract only 10 elements from the result.

function retriveData() {

//declaration of variables ...

axios.get("Firebase link")
    .then((response) => {
        keyArray = Object.keys(response.data);
        let k = shuffle(keyArray); 
        //shuffle is a function to shuffle the key results
        for (var i = 0; i < 10; ++i) {
            current[i] = response.data[k[i]];
        }
    });
return current;} //I want this variable to end up with the promise result in current

I know, this is not how promises work but I need a solution to solve this problem. Thanks!

Madara's Ghost
  • 172,118
  • 50
  • 264
  • 308

2 Answers2

0

axios.get is asynchronous, so either you pass a callback to retrieveData, or it needs to return a Promise itself. There's no way around that.

Using a callback (no error handling):

function retriveData(callback) {
  axios.get("Firebase link")
    .then((response) => {
      keyArray = Object.keys(response.data);
      let k = shuffle(keyArray); 
      //shuffle is a function to shuffle the key results
      for (var i = 0; i < 10; ++i) {
        current[i] = response.data[k[i]];
      }

      callback(null, current);
    });
}

retrieveData((err, result) => console.log(result));

Using a Promise (no error handling):

function retriveData() {
  return new Promise((resolve) => {
    axios.get("Firebase link")
      .then((response) => {
        keyArray = Object.keys(response.data);
        let k = shuffle(keyArray); 
        //shuffle is a function to shuffle the key results
        for (var i = 0; i < 10; ++i) {
          current[i] = response.data[k[i]];
        }

        resolve(current);
    });
}

retrieveData().then((result) => console.log(result));

[EDIT] The above example is mean for illustrative purposes. Since axios.get already returns a Promise, it can be returned back directly from retrieveData.

function retriveData() {
  return axios.get("Firebase link")
    .then((response) => {
      keyArray = Object.keys(response.data);
      let k = shuffle(keyArray); 
      //shuffle is a function to shuffle the key results
      for (var i = 0; i < 10; ++i) {
        current[i] = response.data[k[i]];
      }

      return current;
    });
}

retrieveData().then((result) => console.log(result));
ruiquelhas
  • 1,905
  • 1
  • 17
  • 17
0

Try this: I am making your retriveData function as a promise so you can use it anywhere in your program

function retriveData() {

//declaration of variables ...
return new Promise((resolve, reject) => {

axios.get("Firebase link")
    .then((response) => {
        keyArray = Object.keys(response.data);
        let k = shuffle(keyArray); 
        //shuffle is a function to shuffle the key results
        for (var i = 0; i < 10; ++i) {
            current[i] = response.data[k[i]];
        }
       // if everything fine, if you get any conditional error then call reject();
       resolve(current);
    });
})
}


//call the function like promise
retriveData().then(result => {
//current will come here
     console.log('result comes here');
}).catch(error => {
    //error comes here (reject error)
   console.log('error');

})
radhey shyam
  • 759
  • 6
  • 9