Put the data from the previous promise into the new promise

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

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()?
    });

First, thenI get the results from the firebase database with a promise and make a request for subscription data on user_uidand put the result of this query in promise. But how to relate user_uidto promise?

+4
source share
1 answer

"" promises ( .then, , database), user_uid, promises, , , , , , , Promise.all, 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];
    });
});
+1

Source: https://habr.com/ru/post/1693456/


All Articles