【问题标题】:A promise was created in a handler but was not returned from it在处理程序中创建了一个承诺,但没有从它返回
【发布时间】:2016-03-01 03:17:33
【问题描述】:

我刚刚开始使用 bluebird Promise,遇到了一个令人困惑的错误

代码摘要

var
  jQueryPostJSON = function jQueryPostJSON(url, data) {

    return Promise.resolve(
      jQuery.ajax({
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        type: "POST",
        url: url,
        data: JSON.stringify(data)
      })
    ).then(function(responseData) {
      console.log("jQueryPostJSON response " + JSON.stringify(responseData, null, 2));
      return responseData;
    });
  },
  completeTask = function completeTask(task, variables) {
    console.log("completeTask called for taskId: "+task.id);
    //FIXME reform variables according to REST API docs
    var variables = {
      "action" : "complete",
      "variables" : []
    };
    spin.start();
    return jQueryPostJSON(hostUrl + 'service/runtime/tasks/'+task.id, variables)
      .then(function() {
        gwl.grrr({
          msg: "Completed Task. Definition Key: " + task.taskDefinitionKey,
          type: "success",
          displaySec: 3
        });
        spin.stop();
        return null;
      });
  }

jQueryPostJSON 函数在其他地方使用时似乎可以正常工作,但在这种情况下,服务器会返回数据。

当它在完成任务中使用时,POST 是成功的,可以在服务器端看到,但是 then 函数永远不会在控制台中被调用,而是我得到错误

completeTask called for taskId: 102552
bundle.js:20945 spin target: [object HTMLDivElement]
bundle.js:20968 spinner started
bundle.js:1403 Warning: a promise was created in a  handler but was not returned from it
    at jQueryPostJSON (http://localhost:9000/dist/bundle.js:20648:22)
    at Object.completeTask (http://localhost:9000/dist/bundle.js:20743:14)
    at http://localhost:9000/dist/bundle.js:21051:15
From previous event:
    at HTMLDocument.<anonymous> (http://localhost:9000/dist/bundle.js:21050:10)
    at HTMLDocument.handleObj.handler (http://localhost:9000/dist/bundle.js:5892:30)
    at HTMLDocument.jQuery.event.dispatch (http://localhost:9000/dist/bundle.js:10341:9)
    at HTMLDocument.elemData.handle (http://localhost:9000/dist/bundle.js:10027:28)
bundle.js:1403 Unhandled rejection (<{"readyState":4,"responseText":"","sta...>, no stack trace)

我得到原因的警告,这不是问题所在。 这是未处理的拒绝,事实上 POST 没有错误。

第 21050 行在这里我正在测试这些组合到来自不同模块的功能

jQuery(document).bind('keydown', 'ctrl+]', function() {
        console.log("test key pressed");
        api.getCurrentProcessInstanceTask()
        .then(function(task) {
          api.completeTask(task);
        });

      });

第一个函数调用 api.getCurrentProcessInstanceTask() 的输出似乎表明它工作正常,但无论如何都是这样

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;
          // if(data.data.length > 1){
          //   throw new Error("getCurrentProcessInstanceTask expects single task result. Result listed "+data.data.length+" tasks!");
          // }
          console.log("returning task id: "+data.data[0].id);
          return data.data[0];
        });
    },

【问题讨论】:

  • 你有一个未处理的拒绝实际上是一个警告。请告诉我们Object.completeTask 是什么以及在 21050 行周围可以找到什么。

标签: javascript promise bluebird


【解决方案1】:

您收到警告是因为您 - 正如它所说 - 没有从 then 处理程序返回承诺。
最好通过捕获并记录拒绝来跟踪拒绝的来源。没有堆栈跟踪表明您(或您使用的库之一)是throw 是一个不是Error 的普通对象。尝试找到并修复它。

您的电话应如下所示:

api.getCurrentProcessInstanceTask().then(function(task) {
    return api.completeTask(task);
//  ^^^^^^
}).catch(function(err) {
// ^^^^^
    console.error(err);
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-02
    • 1970-01-01
    • 2016-09-20
    • 2016-08-23
    相关资源
    最近更新 更多