0

I have a callable Cloudfunction that should fetch multiple data. The keys where to fetch from are calculated by the function.

I have an array like this

var keys = ["key1", "key2, "key5"]

The problem is that the length of the keylist is variable and i want to send all the datas gathered from the database at the given key back to the user.

Something like this:

result = {
  key1: value,
  key2: value,
  key5: value,
}

The database only gives out Promisses that are not asnyc. How do I make sure that i only give out data, once all the data are gathered. Something like

admin.database().ref('/path/key1').once('value').then( snapshot => {
  admin.database().ref('/path/key2').once('value').then ( snapshot => {
    ...
  }
}

Won't work, because the number of keys is variable.

4
  • That all sounds doable. What's the problem? Where along the path of implementing this did you get stuck? Commented May 10, 2018 at 15:00
  • As said i only get Promise variables and i can't use await, to wait for the result to add all the things together Commented May 10, 2018 at 15:01
  • Please update your question to contain the minimal code that reproduces whats you tried and where you got stuck. Without that it's hard to help beyond writing the code for you, which is not what Stack Overflow is about. Commented May 10, 2018 at 15:02
  • thanks @FrankvanPuffelen i was just about to do that. Commented May 10, 2018 at 15:08

1 Answer 1

1

I managed to find a solutoion to my Problem

I had to write a recursive function to chain the promises

function fetchData(keyArray) {
  var index = 0;
  var fetchedValues = {};

  return new Promise(function(resolve, reject) {
    function next(fetchedValues) {
      if (index < keyArray.length) {
        newFetchedValues = fetchedValues;
        admin.database().ref('/path/to/key/' + keyArray[index]).once('value').then(((snapshot) => {
          newFetchedValues[keyArray[index]] = snapshot.val();
          index++;
          next(newFetchedValue);
        }), reject);
      } else {
        resolve(fetchedValues);
      }
    }
    next(fetchedValues);
  });
}

This function returnes a promise object, that can states resolved, once all the Values in the array are Fetched. This allows synchronous Fetching of a dynamic number of Values.

I solved this by following the first answer of this question How to synchronize a sequence of promises?

Sign up to request clarification or add additional context in comments.

2 Comments

This could be a lot cleaner if you collect all the promises from calls to once() in an array, then use Promise.all() to receive all the results in another array.
Cool thanks man that was the thing that i was looking for when i found my solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.