简体   繁体   English

Javascript模块化承诺链乱序

[英]Javascript Modularized Promise Chain Out of Order

I am trying to figure out the reason why my promise chain is executing out of order despite writing a non-nested then chain. 我试图找出为什么我的诺言链无法执行的原因,尽管编写了一个非嵌套的然后链。 My functions have been modularized to reduce the code bloat that will happen in my chain (I expect to have five then methods) and I'm not sure if something within those modules are causing the jump in the order or if it is due to my overall promise structure. 我的功能已经过模块化,以减少链中发生的代码膨胀(我希望有五个then方法),而且我不确定这些模块中的某些内容是否会导致顺序跳转,或者是否是由于我的原因总体承诺结构。

Here is the terminal output: 这是终端输出:

Executing (a6bf615e-5497-47b2-8aea-3f7d70927cba): START TRANSACTION;
Executing (a6bf615e-5497-47b2-8aea-3f7d70927cba): SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ;
Step 1: Document Find
Step 3: Cancel Stripe Subscription
string //console.log(typeof subscriptionId);
sub_jsdfjsdfjdsfjsdfj //console.log(subscriptionId)
Executing (a6bf615e-5497-47b2-8aea-3f7d70927cba): SELECT ....
Step 2: Set Array
[ 13, 14 ] //console.log(documentArr);
undefined //console.log(canceledStripeAccount);
undefined //console.log(canceledAt)

Here is my promise structure: 这是我的诺言结构:

var sequelize = require('sequelize');
var models = require('../../../models/db-index');
var deleteAccount = require('./delete-account');


//DELETE /settings/account
exports.delete = function(req, res){
    var documents;
    var documentArr;
    var canceledStripeAccount;
    var canceledAt;

    return models.sequelize.transaction().then(function(t){

        return deleteAccount.queryAllDocuments(req.session.organizationId, t)
        .then(function(_document){
                console.log("Step 2: Set Array");
                documentArr = _document;
                console.log(documentArr);
        })
        .then(deleteAccount.cancelStripeAccount(req.session.subscriptionId, canceledStripeAccount, canceledAt))
        .then(function(canceledStripeAccount){
            console.log(canceledStripeAccount);
            console.log(canceledAt)
        });
    });
};

Modules File: 模块文件:

var models = require('../../../models/db-index');
var components = require('./components');
var stripe = require('stripe')(process.env.STRIPE_API_KEY);

module.exports = {

    queryAllDocuments: function(organization, t){
        console.log("Step 1: Document Find");
        return models.Document.findAll({
            include: [{
                model: models.User,
                include: [{
                    model: models.Organization,
                    where: {
                        organizationId: organization
                    }
                }]
            }],
            transaction: t
        })
    },

    cancelStripeAccount: function(subscriptionId, canceledStripeAccount, canceledAt){
        console.log("Step 3: Cancel Stripe Subscription");
        console.log(typeof subscriptionId);
        console.log(subscriptionId)
        return stripe.subscriptions.del(subscriptionId).then(function(_canceledStripeAccount){
            return canceledStripeAccount = _canceledStripeAccount;
            //canceledAt = canceledStripeAccount.canceled_at;
        })
    }

}

You are calling the function directly before .then() even calls its callback (and passing the return value from that to .then() ): 您正在直接在.then()甚至调用其回调之前调用该函数(并将返回值从该回调传递给.then() ):

.then(deleteAccount.cancelStripeAccount(req.session.subscriptionId, 
        canceledStripeAccount, canceledAt))

Instead, .then() should be passed a function reference so it can call the function sometime later: 相反, .then()应该传递一个函数引用,以便稍后可以调用该函数:

.then(() => deleteAccount.cancelStripeAccount(req.session.subscriptionId,
              canceledStripeAccount, canceledAt))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM