我刚刚开始使用带有节点,require和browserify的js模块并尝试获取代码来工作,最初是在单个脚本中。在这种情况下应用PromiseJS
我也想开始使用promise,但我不确定如何在这种情况下应用它。
所有需要browserify部分似乎工作,所以我会把所有没有相关位为简单起见。
在一个模块中我有这样的事情
module.exports.api = function(){
var getCurrentProcessInstanceTask = function getCurrentProcessInstanceTask(options) {
if (!currentProcess || !currentProcess.id) {
throw new Error("no currentProcess is set, cannot get active task");
return;
}
var processInstanceId = currentProcess.id;
jQuery.get(hostUrl + "service/runtime/tasks", {
processInstanceId: processInstanceId
})
.done(function(data) {
console.log("response: " + JSON.stringify(data, null, 2));
currentProcess.tasks = data.data;
if (options && options.callback) {
options.callback(data.data);
}
});
}
return {
getCurrentProcessInstanceTask: getCurrentProcessInstanceTask
}
}
那么其他模块中我有这样的事情
var Api = require('./api');
module.exports.view = function(){
var api = Api();
var setupEmbeddedView = function setupEmbeddedView(url, tmpl) {
tmpls.renderExtTemplate({
name: tmpl,
selector: targetDiv,
data: {
url: url,
width: iframeTargetDiv.width(),
height: iframeTargetDiv.height()
},
callback: function() {
jQuery('#taskFormFrame').load(function(e) {
console.log("taskFormFrame load fired!");
});
}
});
},
showCurrentTaskForm = function showCurrentTaskForm() {
console.log("mark");
api.getCurrentProcessInstanceTask({
callback: function(tasks) {
setupEmbeddedView(getTaskFormUrl(tasks), 'showTaskForm');
}
});
}
return {
showCurrentTaskForm: showCurrentTaskForm
}
}
调用showCurrentTaskForm在另一个模块中,其中视图需要的结果的API .getCurrentProcessInstanceTask正在执行的部分,但setupEmbeddedView永远不会被调用。
我很困惑为什么也许有人可以解释。
另外,我想我会如何应用promisejs在这种特殊情况下,而不是使用回调链职能