Javascript模块化承诺链故障

时间:2017-07-07 00:15:32

标签: javascript promise

我试图弄清楚为什么我的保证链无序执行,尽管写了一个非嵌套的然后链。我的函数已经模块化,以减少将在我的链中发生的代码膨胀(我希望有五个方法),我不确定这些模块中的某些东西是否导致顺序跳转或者是否是由于我的整体承诺结构。

这是终端输出:

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)

这是我的承诺结构:

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

模块文件:

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

}

1 个答案:

答案 0 :(得分:2)

您在.then()调用其回调之前直接调用该函数(并将返回值传递给.then()):

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

相反,.then()应该传递一个函数引用,以便稍后调用该函数:

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