简体   繁体   English

Swagger的Bluebird Promise:return不是函数

[英]Bluebird Promise with Swagger: return is not a function

I'm using Swagger to create my JS API client for calling a set of REST endpoints. 我正在使用Swagger创建用于调用一组REST端点的JS API客户端。 I'm configuring the Swagger client to use Promises ( new Swagger({ spec: spec, usePromise: true }) ). 我正在配置Swagger客户端以使用Promises( new Swagger({ spec: spec, usePromise: true }) )。

I require Bluebird before Swagger, to use Bluebird promises. require Swagger之前的Bluebird才能使用Bluebird Promise。 But my app is blowing up when trying to use Promise.return . 但是尝试使用Promise.return时我的应用程序Promise.return

The code looks like 代码看起来像

client.user.get_db_user(db).return("foo");

where client is the Swagger reference. 其中client是Swagger参考。

The error I get is 我得到的错误是

app: TypeError: client.user.get_db_user(...).return is not a function

It works if I replace return with then(() => value) . 如果我用then(() => value)替换return ,它将起作用。 return is supposed to be shorthand for this sort of use of then . return应该是对then这种使用的简写。

After investigating, I see it's failing on promises returned by the Swagger client. 经过调查,我发现Swagger客户返回的诺言失败了。

What's happening? 发生了什么?

It doesn't appear that you can tell Swagger to use Bluebird promises. 看来您无法告诉Swagger使用Bluebird Promise。 But, in specific places where you want to use Bluebird's methods, you can "cast" a Swagger promise to a Bluebird promise and then use Bluebird's features. 但是,在要使用Bluebird方法的特定位置,您可以将Swagger Promise“投射”到Bluebird Promise,然后使用Bluebird的功能。 To convert a promise like that, you just wrap it in Promise.resolve() : 要转换这样的承诺,只需将其包装在Promise.resolve()

const Promise = require('bluebird');

Promise.resolve(client.user.get_db_user(db)).return(...)

Although in this specific case, since .return() is just there to save you some typing, it isn't really a net savings after you've wrapped the previous promise in Promise.resolve() . 尽管在此特定情况下,由于.return()只是在为您节省一些键入时间,因此在您将先前的承诺包装在Promise.resolve()之后,这实际上并不是净节省。 But, for more meaningful methods like .map() or something like that, it might be more useful. 但是,对于更有意义的方法,例如.map()或类似的方法,它可能会更有用。

It turns out that Swagger uses the Q promise library explicitly under the hood. 事实证明,Swagger在后台显式使用了Q promise库。

Bluebird and Q are partially compatible, so many things work fine. 蓝鸟和Q是部分兼容的,所以很多事情都可以正常工作。 The problem happens when trying to use parts of the Bluebird API that Q doesn't have. 尝试使用Q所没有的Bluebird API的部分时,会发生问题。

In particular, Bluebird has "return", whereas Q does not. 特别是,蓝鸟有“返回”,而Q没有。

The easiest answer is to use the then construction discussed in the question. 最简单的答案是使用then在这个问题讨论建设。

Other possible solutions I have yet to try: 我尚未尝试的其他可能解决方案:

  1. Use the "bluebird-q" project to replace Q with Bluebird. 使用“ bluebird-q”项目将Q替换为Bluebird。
  2. promisifyAll Swagger instead of using Swagger's built-in promise support. promisifyAll Swagger,而不是使用Swagger的内置promise支持。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM