RicoSuter / NSwag

The Swagger/OpenAPI toolchain for .NET, ASP.NET Core and TypeScript.
http://NSwag.org
MIT License
6.61k stars 1.22k forks source link

Async / Await support for TypeScript Angular2 #660

Open marc-mueller opened 7 years ago

marc-mueller commented 7 years ago

We are currently defining our implementation stack and we'd like to use NSwag to generate multiple client packages for our Angular application.

One thing with is being discussed right now is if we should use async / await for the Angular implementation. If so, it would be handy if there was a TypeScript template providing the client implementation with async methods. Is this on the roadmap?

The idea is basically to convert the Observable to a Promise to be able to declare the method as async:

getXY(): Promise<XY[]> {
  return this.http.get(this.xyUrl)
    .toPromise()
    .then(response => response.json().data as XY[])
    .catch(this.handleError);
}
RicoSuter commented 7 years ago

Hoi Marc :)

Async/await is already supported by the generated clients, just call:

let result = await client.getXY().toPromise();

Maybe we should add an option "UsePromises" to call this method internally... or generate a second method "getXYAsync()" which returns a promise?

(it would be cool if Observable would just inherit from Promise...)

marc-mueller commented 7 years ago

Hi Rico

Thanks for the quit response. Yes, we could already use it with the current version by applying the toPromise() function to the call. My understanding of a client code generator is that it generates the code we need and implements all the "boilerplate code" we would repetitively add to the usage of the client. Therefore the option "UsePromises" would be great to be directly implemented into the generator.

I'm currently not sure if generating both versions in the same class or just generate one or the other would be the better solution. Normally you define one pattern on how to interact / write code and therefore a switch for one or the other version would better leverage that situation.

jsommr commented 7 years ago

Calling toPromise() will make it impossible to cancel the request, since promises doesn't support cancellation.

RicoSuter commented 7 years ago

@nerfpops you are right, but we can still provide an option for that...

ken2k commented 4 years ago

Hi @RicoSuter Is this option available yet? Thanks

oguzhankahyaoglu commented 2 years ago

any update on this issue? since topromise method is deprecated as of angular12 and will be removed soon..