承诺是在处理程序中创建的,但未从中返回 [英] A promise was created in a handler but was not returned from it

查看:100
本文介绍了承诺是在处理程序中创建的,但未从中返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始使用蓝鸟承诺而且收到了令人困惑的错误

I've just started using bluebird promises and am getting a confusing error

代码摘要

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函数似乎在使用其他地方时工作正常,但在那种情况下有从服务器返回的数据。

The jQueryPostJSON function seems to work fine as is when used else where, but in that case there is data returned from the server.

当它在完成任务中使用时,POST是成功的,如在服务器端可以看到的,但是then函数是从来没有在控制台中调用我得到错误

When it's used within complete task, the POST is successful as can be seen on the server side, but the then function is never called instead in the console I get the error

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没有错误。

The warning I get the reason for, that's not the issue. It's the Unhandled rejection and the fact that there was in fact no error from the POST.

21050行在这里我正在测试这些功能与单独模块的组合

line 21050 is here I am testing the combination of these to functions from separate modules

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

      });

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

Output from the first function call api.getCurrentProcessInstanceTask() seems to indicate it is working correctly, but here it is anyway

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];
        });
    },


推荐答案

你得到了警告,因为你 - 正如它所说 - 没有从然后处理程序返回承诺。

拒绝来自哪里最好通过捕获来跟踪它并记录它。没有堆栈跟踪表明您(或您使用的其中一个库)是抛出一个不是错误的普通对象。尝试找到并修复它。

You're getting the warning because you are - as it says - not returning the promise from the then handler.
Where the rejection is coming from would best be tracked by catching it and logging it. That there is no stack trace suggests that you (or one of the libs you use) is throwing a plain object that is not an Error. Try finding and fixing that.

您的电话应如下所示:

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

这篇关于承诺是在处理程序中创建的,但未从中返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆