使用promises作为数据依赖关系的正确方法

时间:2014-12-03 08:51:20

标签: javascript promise chaining

我试图在此处阅读文章重建流程:https://blog.jcoglan.com/2013/03/30/callbacks-are-imperative-promises-are-functional-nodes-biggest-missed-opportunity/

下面的代码抛出一个错误,我现在意识到因为theResults.org在进程尝试使用theResults.org.then()时并不是一个承诺,但你可能会看到我尝试的是什么实现。我想要设置所有内容,以便不是指示控制流,而是让子流程依赖于进入承诺的数据,但我对如何获得一些链接感到失望。

验证后,必须创建组织,然后创建组织,创建上下文等等。我知道我在这里错过了一个关键的理解 - 任何人都可以向我指出步骤或语法I&#39我好错了吗?     '使用严格的'

var q = require('q');

module.exports["purchaseSchool"] = function(req, successCB, failCB) {
    try {
    //var purchaseData = req.body.formData;
    var purchaseData = "";
    var theResults = {
        bValidated: null,
        org: null,
        context: null,
        cohort: null,
        invoice: null,
        bInvoiced: null,
        bEmailed: null
    }
    // validate the data
    theResults.bValidated = validatePurchaseData(purchaseData);

    theResults.bValidated.then(function () {
        // DEBUG: remove this
        console.log("validated");
        theResults.org = createOrg(purchaseData);
    });

    theResults.org.then(function() {
        // DEBUG: remove this
        console.log("org");
        theResults.context = createContext(purchaseData, theResults.org);
    });

    theResults.context.then(function() {
        // DEBUG: remove this
        console.log("context");
        theResults.cohort = createCohort(purchaseData, theResults.context);
    });

    theResults.cohort.then(function() {
        // DEBUG: remove this
        console.log("cohort");
        theResults.invoice = createInvoice(purchaseData);
    });

    theResults.invoice.then(function() {
        // DEBUG: remove this
        console.log("invoice");
        theResults.bInvoiced = sendInvoice(theResults.invoice);
    });

    theResults.bInvoiced.then(function() {
        // DEBUG: remove this
        console.log("invoice sent");
        theResults.bEmailed = sendPurchaseEmail(purchaseData);
    });

    theResults.bEmailed.then(function() {
        // DEBUG: remove this
        console.log("emailed");
        successCB("Purchase Complete");
    });
    } catch (err) {
        console.log(err);
        console.log(err.stack);
        failCB();
    }
};

function validatePurchaseData(data) {
    var defer = q.defer();

    setTimeout(function(){ defer.resolve(true) }, 5000);

    return defer.promise;
}

function createOrg(org) {
    var defer = q.defer();

    setTimeout(function(){ defer.resolve({id:"org"}) }, 5000);

    return defer.promise;
}
function createContext(data, org) {
    var defer = q.defer();

    setTimeout(function(){ defer.resolve({id:"context"}) }, 5000);

    return defer.promise;
}
function createCohort(data, context) {
    var defer = q.defer();

    setTimeout(function(){ defer.resolve({id:"cohort"}) }, 5000);

    return defer.promise;
}
function createInvoice(data) {
    var defer = q.defer();

    setTimeout(function(){ defer.resolve({id:"invoice"}) }, 5000);

    return defer.promise;
}
function sendInvoice(invoice) {
    var defer = q.defer();

    setTimeout(function(){ defer.resolve(true) }, 5000);

    return defer.promise;
}
function sendPurchaseEmail(data) {
    var defer = q.defer();

    setTimeout(function(){ defer.resolve(true) }, 5000);

    return defer.promise;
}

1 个答案:

答案 0 :(得分:0)

通常情况下,提出问题有助于我找到答案。可能有更优雅的方法来做到这一点,也许使用q.all来处理最终的回报(我希望听到任何更好的指示)但是这就是它的工作原理:

'use strict'

var q = require('q');

module.exports["purchaseSchool"] = function (req, successCB, failCB) {
    // DEBUG: remove this
    console.log("blah1");
    try {
        //var purchaseData = req.body.formData;
        var purchaseData = "";
        var theResults = {
            bValidated: null,
            org: null,
            context: null,
            cohort: null,
            invoice: null,
            bInvoiced: null,
            bEmailed: null
        }
        // validate the data
        theResults.bValidated = validatePurchaseData(purchaseData);

        theResults.org = theResults.bValidated.then(function (bValidated) {
            // DEBUG: remove this
            console.log("blah2");
            return createOrg(purchaseData);
        });

        theResults.context = theResults.org.then(function (org) {
            return createContext(purchaseData, org);
        });

        theResults.cohort = theResults.context.then(function (context) {
            return createCohort(purchaseData, context);
        });

        theResults.invoice = theResults.cohort.then(function (cohort) {
            return createInvoice(purchaseData);
        });

        theResults.bInvoiced = theResults.invoice.then(function (invoice) {
            return sendInvoice(invoice);
        });

        theResults.bEmailed = theResults.bInvoiced.then(function (bInvoiced) {
            return sendPurchaseEmail();
        });

        theResults.bEmailed.then(function (bEmailed) {
            successCB("Purchase Complete");
        });
    } catch (err) {
        console.log(err);
        console.log(err.stack);
        failCB();
    }
};

function validatePurchaseData(data) {
    var defer = q.defer();

    setTimeout(function () {
        defer.resolve(true)
    }, 1000);

    return defer.promise;
}

function createOrg(org) {
    var defer = q.defer();

    setTimeout(function () {
        defer.resolve({id: "org"})
    }, 1000);

    return defer.promise;
}
function createContext(data, org) {
    var defer = q.defer();

    setTimeout(function () {
        defer.resolve({id: "context"})
    }, 1000);

    return defer.promise;
}
function createCohort(data, context) {
    var defer = q.defer();

    setTimeout(function () {
        defer.resolve({id: "cohort"})
    }, 1000);

    return defer.promise;
}
function createInvoice(data) {
    var defer = q.defer();

    setTimeout(function () {
        defer.resolve({id: "invoice"})
    }, 1000);

    return defer.promise;
}
function sendInvoice(invoice) {
    var defer = q.defer();

    setTimeout(function () {
        defer.resolve(true)
    }, 1000);

    return defer.promise;
}
function sendPurchaseEmail(data) {
    var defer = q.defer();

    setTimeout(function () {
        defer.resolve(true)
    }, 1000);

    return defer.promise;
}