2015-11-25 177 views
0

我刚刚开始使用带有节点,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在这种特殊情况下,而不是使用回调链职能

回答

0

当前的jQuery的版本不履行承诺/ A +规格的例子。我会推荐使用单独的承诺库,例如bluebird

运行异步代码块的每个函数都应该返回一个promise。

看着你的代码,getCurrentProcessInstanceTask,因此它应该返回一个承诺。

由于您使用的是返回jQuery承诺的jQuery.get(),因此您必须让蓝鸟同化该承诺来创建合适的承诺链。

var getCurrentProcessInstanceTask = function getCurrentProcessInstanceTask() { 

    if (!currentProcess || !currentProcess.id) { 
     return Promise.reject(new Error("no currentProcess is set, cannot get active task")) 
    } 

    var processInstanceId = currentProcess.id; 

    return Promise.resolve(jQuery.get(hostUrl + "service/runtime/tasks", {processInstanceId: processInstanceId}) 
     .then(function(data){ 
     console.log("response: " + JSON.stringify(data, null, 2)); 
     currentProcess.tasks = data.data; 
     return data.data; 
     }); 
    ); 
} 

按顺序运行它,只需修改showCurrentTaskForm函数调用API函数和.then()返回的承诺。

showCurrentTaskForm = function showCurrentTaskForm() { 
    console.log("mark"); 
    api.getCurrentProcessInstanceTask() 
     .then(function(result){ 
     setupEmbeddedView(result, 'showTaskForm'); 
     }) 
     .catch(function(e){ 
     //handle the error 
     }); 
}