【问题标题】:a promise was created in a handler but was not returned from it in bluebird在处理程序中创建了一个承诺,但在蓝鸟中没有从它返回
【发布时间】:2016-09-20 18:16:57
【问题描述】:

我在我的应用程序中使用bluebird,并使用 babel 将我的代码编译成 es5。但是,我总是收到这个警告,并且我已经检查过 Promise 的每一部分都有 return 值。

这是我的代码:

Promise.promisifyAll(fs);

use.login().then((result) => {
  console.log(result);
  doSomething('../test.png');
});

function doSomething(filepath) {
  return fs.readFileAsync(filepath).then((bufs) => (
      doPost(url, bufs, filepath)
        .then((res) => (
          res.error ? Promise.reject(res.error) : Promise.resolve(res))
        )
  )).catch((err) => {
    console.error(err);
    return err;
  });
}


function doPost(url, bufs = null, filepath = null) {
  return new Promise((resolve, reject) => (
    unirest.post(url)
      .headers(config.Headers)
      .timeout(120000)
      .field(bufs)
      .attach('files', filepath)
      .end((res) => (
        res.error ? reject(res.error) : resolve(res)
      ))
  ));
}

错误信息详情:

Warning: a promise was created in a handler but was not returned from it
at doSomething (/home/test/Documents/test/lib/abc.js:2:27)
// This line number is referred to the compiled code which is equal to line 4:12 in the above code

【问题讨论】:

  • 你在哪里打电话doSomething?这是你的全部代码吗?
  • @Bergi 不,这不是我的全部代码,但我只收到上述代码中的警告。当我需要读取文件时,我会打电话给doSomething,例如。 doSomething('../test.png')
  • 我的意思是,也许你是从一个没有 return 任何东西的 Promise 处理程序调用 doSomething。这是警告的整个调用堆栈吗?如果您能显示整个代码,将会很有帮助。
  • 哦!!! @Bergi 是的,你是对的!!!我的 doSomething 调用堆栈中没有 return 任何东西!在doSomething 之后添加return 后警告消失了。非常感谢 !!我已经更新了上面的代码以显示我的代码,这将导致警告。

标签: javascript promise bluebird


【解决方案1】:

感谢@Bergi,我没有检查我调用doSomething 的承诺堆栈,所以在我修改我的代码后警告消失了

use.login().then((result) => {
  console.log(result);
  doSomething('../test.png');
  return null; // Add this line to make promise return anything, then the warning gone
});

【讨论】:

    猜你喜欢
    • 2016-08-23
    • 1970-01-01
    • 2016-03-01
    • 1970-01-01
    • 2016-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多