【发布时间】: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