簡體   English   中英

正確使用Promise作為數據依賴項的方法

[英]Proper way to use promises as data dependencies

我正在嘗試根據此處的文章重建流程: https : //blog.jcoglan.com/2013/03/30/callbacks-are-imperative-promises-are-functional-nodes-biggest-missed-opportunity /

我現在意識到下面的代碼會引發錯誤,因為在過程嘗試使用ResultsResults.org.then()時,Results.org不是一個承諾,但是您可能可以看到我正在嘗試實現的目標。 我想進行所有設置,以便使我不再依賴於控制流,而只是使子流程依賴於進入promise的數據,但是我迷失了如何正確地進行一些鏈接。

驗證之后,必須先創建該組織,然后再創建它,並創建上下文等。我知道我在這里缺少關鍵的了解-有人可以指出我錯誤的步驟或語法嗎? “使用嚴格”

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

通常,提出問題可以幫助我找到答案。 可能有更優雅的方法可以執行此操作,也許使用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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM