使用承诺作为数据依赖项的正确方法
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/
下面的代码抛出了一个错误,我现在意识到,因为当进程尝试使用 theResults.org.then() 时 theResults.org 这不是一个承诺,但你可能会看到我想要实现的目标。我想设置所有内容,以便我只是使子流程依赖于进入承诺的数据,而不是指示控制流,但我迷失了如何正确链接。
验证后,必须创建组织,然后当它存在时,创建上下文等。我知道我在这里缺少一个关键的理解 - 任何人都可以向我指出我弄错的步骤或语法吗? "使用严格"
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;
}
相关文章:
- 序列化数据属性中对象的最可靠方法
- 有没有一种方法可以仅使用HTML/CSS来隐藏基于特定值的数据单元格
- 如何在从客户端接收数据时从本机方法触发javascript函数?
- 我们可以用参数对象集合而不是原始数据来调用JavaScript collection.reduce()方法吗
- 在put方法之前从作用域获取数据
- 使用ReactJS和Flux架构从服务器获取数据的正确方法是什么
- 有没有一种方法可以使用Javascript检索谷歌表单ITSELF(而不是电子表格)的数据
- 有没有一种简单的方法可以用Lodash映射嵌套数据
- 数据属性有没有更干净的方法
- 将数据从promise then方法传递到对象方法
- 有没有更好的方法可以测量从Ajax调用返回数据所需的总时间
- 如何正确处理依赖ViewChild和Angular 2中可观察到的数据的组件上的更改检测
- 从javascript调用codeigniter方法并传递数据
- 如何管理原始对象之间的数据依赖关系
- 在jquery mobile中,post方法数据发送不正确
- 使用承诺作为数据依赖项的正确方法
- Mootools存储方法:数据存储在哪里
- 当javascript被禁用时,保护表单的最佳方法是依赖javascript验证
- 我如何用骨干MVC表达数据依赖关系
- 模块中的数据依赖