javascript - 将先前 promise 中的数据放入新 promise 中

标签 javascript node.js promise

<分区>

我需要将上一个 promise 结果的数据放到下一个 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()?
    });

首先 然后 我从 firebase 数据库中获得了 promise 的结果,并请求获取有关 user_uid 订阅的数据,并将此请求的结果放入 promise 。但是如何将 user_uidpromise 联系起来呢?

最佳答案

您可以在 promises 范围之外创建一个“carriage”数组(因此它在所有 .then block 中可用,在您的情况下,您正在创建 database 对象),将 user_uid 推送到您使用它们创建 promise 的位置,然后将它们撤回您需要的位置,只需确保回车数组与您传递的数组具有相同的索引到 Promise.all 调用,以便将 id 映射到相应的 promise:

// 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];
    });
});

关于javascript - 将先前 promise 中的数据放入新 promise 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48721554/

相关文章:

javascript - 我如何在 dart 中实现这个 jquery 模式?

javascript - 防止持久状态锁定 VueX 中的应用程序

javascript - 需要根据 ASP.net 中的 Controller 属性设置链接的可见性 : MVC

Javascript string.match 拒绝返回包含多个匹配项的数组

javascript - Azure Function 不通过 context.res 返回响应

javascript - 执行错误 ReactDOM.render(<App/>, document.getElementById ('root' ));

javascript - 使用node js连接SQL Server时出现问题?

javascript - 等待 redux 状态满足条件

javascript - 如何使用 javascript 验证 jsp 表单(提交时)中的多个文本框?

javascript - 如何排序导致 nodejs - mongodb 搜索,但是,通过调用动态方法