Bluebird - Promise.resolve 不起作用,但 return 有效
Bluebird - Promise.resolve doesn't work but return works
我有这个功能returns一个承诺
save(data) => {
return mymongo.insert(data).then(
(result) => {
console.log('hello mongo');
console.log(result);
return Promise.resolve(result);
}
)
}
在我的路由器中我调用了那个函数
app.post('/printers', (req, res, next) => {
save(req.body).then(
(result) => {
console.log('hello') // is not called, nothing is printed
console.log(result) // is not called
res.send(result)
}
).catch(res.send);
});
然后使用 curl 我执行请求
curl -v -d '{ ... }' -X POST http://localhost:8080/printers
它非常快而且看起来不错
* Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 8080 (#0)
> POST /printers HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.52.1
> Accept: */*
> Content-Length: 35
> Content-Type: application/x-www-form-urlencoded
>
* upload completely sent off: 35 out of 35 bytes
< HTTP/1.1 200 OK
< X-Powered-By: Express
< Content-Type: application/json; charset=utf-8
< Content-Length: 2
< ETag: W/"2-vyGp6PvFo4RvsFtPoIWeCReyIC8"
< Date: Fri, 21 Apr 2017 01:01:30 GMT
< Connection: keep-alive
<
* Curl_http_done: called premature == 0
* Connection #0 to host localhost left intact
所以没有超时但没有响应,在控制台中我只看到来自 save()
的日志
hello mongo
{ .... }
当我将 return Promise.resolve(result)
替换为 return result
时,我收到了服务器的响应并且 console.log(result)
被执行,在我的控制台中我看到了
hello mongo
{ .... }
hello
{ .... }
这可能是 mongodb 承诺实现的问题,因为 insert(...).then
回调中的 return 值由该实现处理。我没有找到它符合 Promise/A+ 的任何参考资料,而您的代码似乎证明它不符合。
如果您关心的是使用您喜欢的 Promise API(在您的情况下可能是蓝鸟),那么请在较早的阶段进行转换,这样您就不会依赖 mongodb' s then
实施:
save(data) => {
return Promise.resolve(mymongo.insert(data)).then(
(result) => {
console.log('hello mongo');
console.log(result);
return result;
}
)
}
我有这个功能returns一个承诺
save(data) => {
return mymongo.insert(data).then(
(result) => {
console.log('hello mongo');
console.log(result);
return Promise.resolve(result);
}
)
}
在我的路由器中我调用了那个函数
app.post('/printers', (req, res, next) => {
save(req.body).then(
(result) => {
console.log('hello') // is not called, nothing is printed
console.log(result) // is not called
res.send(result)
}
).catch(res.send);
});
然后使用 curl 我执行请求
curl -v -d '{ ... }' -X POST http://localhost:8080/printers
它非常快而且看起来不错
* Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 8080 (#0)
> POST /printers HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.52.1
> Accept: */*
> Content-Length: 35
> Content-Type: application/x-www-form-urlencoded
>
* upload completely sent off: 35 out of 35 bytes
< HTTP/1.1 200 OK
< X-Powered-By: Express
< Content-Type: application/json; charset=utf-8
< Content-Length: 2
< ETag: W/"2-vyGp6PvFo4RvsFtPoIWeCReyIC8"
< Date: Fri, 21 Apr 2017 01:01:30 GMT
< Connection: keep-alive
<
* Curl_http_done: called premature == 0
* Connection #0 to host localhost left intact
所以没有超时但没有响应,在控制台中我只看到来自 save()
的日志hello mongo
{ .... }
当我将 return Promise.resolve(result)
替换为 return result
时,我收到了服务器的响应并且 console.log(result)
被执行,在我的控制台中我看到了
hello mongo
{ .... }
hello
{ .... }
这可能是 mongodb 承诺实现的问题,因为 insert(...).then
回调中的 return 值由该实现处理。我没有找到它符合 Promise/A+ 的任何参考资料,而您的代码似乎证明它不符合。
如果您关心的是使用您喜欢的 Promise API(在您的情况下可能是蓝鸟),那么请在较早的阶段进行转换,这样您就不会依赖 mongodb' s then
实施:
save(data) => {
return Promise.resolve(mymongo.insert(data)).then(
(result) => {
console.log('hello mongo');
console.log(result);
return result;
}
)
}