4

I need to put data from previous promise result to the next promise result.

var database = admin.database().ref('/schema-v0_1/objects/subscription');
    return database.once("value").then(function(snapshot) {
        var array_user_subs = [];
        snapshot.forEach(function(data) {
            var user_uid = data.key;
            var database_user_subs = admin.database().ref('/schema-v0_1/objects/subscription');
            var promise = database_user_subs.child(user_uid).once("value", function(snapshot) {
            }, function(error) {
            });
            array_user_subs.push(promise);
        });
        return Promise.all(array_user_subs);
    }).then(function(array_user_subs) {
        var array_subs_info = [];
        array_user_subs.forEach(function(data) {
            var user_uid = data.key;

            data.forEach(function(snapshot) {
                var gpa = snapshot.key;
                var token_purchase = snapshot.val().token;
                var sku_purchase = snapshot.val().sku;

                var promise = promisify(publisher.purchases.subscriptions.get)({
                    packageName: 'com.example',
                    subscriptionId: sku_purchase,
                    token: token_purchase
                });

                //how to linked user_uid with promise?
                array_subs_info.push(promise);
            });
        }
        return Promise.all(array_subs_info);
    }).then(function(array_subs_info) {
        //here I get array with results of promise, but how to get linked with result user_uid from previous promise in previous then()?
    });

In first then I get results from firebase database with promise and make request for get data about subscriptions for user_uid, and put result of this request into promise. But how to linked user_uid with promise?

  • You can thread (still not sure what the correct terminology is) results of a function to another. Look [here](https://stackoverflow.com/a/48677470/1641941) under update. Another option is to just squeeze all implementation into one function that will automatically share results through closure scope. Or use async function syntax. – HMR Feb 10 '18 at 18:13

1 Answers1

1

You can create a "carriage" array outside the promises scope (so it's available inside all the .then blocks, in your case on the same level you're creating the database object), push the user_uids where you are creating the promises using them and then withdraw them where you need them, just make sure the carriage array has the same indexes as the array you are passing to the Promise.all call so the ids are mapped to corresponding promises:

// outer scope:
var database = whatever;
var user_uid_list = [];

// inside the 1st .then call:
    array_subs_info.push(promise);
    // store the uid with the same index as the promise has
    user_uid_list[array_subs_info.length - 1] = user_uid;

// the final .then call:
}).then(function(array_subs_info) {
    array_subs_info.forEach(function (data, index) {
        // withdraw the uid with the right index
        var user_uid = user_uid_list[index];
    });
});
Tomas Varga
  • 1,432
  • 15
  • 23