improbable-eng / ts-protoc-gen

Protocol Buffers Compiler (protoc) plugin for TypeScript and gRPC-Web.
Apache License 2.0
1.36k stars 173 forks source link

Service Client generated doesn't support Promise response #243

Open chin2km opened 4 years ago

chin2km commented 4 years ago

The generated service client doesn't give a Promise response, instead, it requires callbacks for handling the response from the server.

Versions of relevant software used latest

What happened This is a sample generated client code. https://github.com/improbable-eng/ts-protoc-gen/blob/master/examples/generated-grpc-web/proto/examplecom/simple_service_pb_service.d.ts

From the types you can see the when you execute a generated client, you need to pass the request object and a callback as a second parameter to handle the response.

What you expected to happen

Expects that when you execute a service client, it give a promise response instead

auzhva commented 4 years ago

To add to this,

There is an old issue in the tracker as #59 which is stated that it will not be fixed. Mainly because willingness not to break compatibility with official node.

And,

Since recently official client added support for client_type=PromiseClient. See https://github.com/grpc/grpc-web/pull/354 And this is now part of official node release.

So would it be possible to reconsider #59 and possibly provide something similar here?

Thank you,

roboslone commented 3 years ago

+1 Promise-based client would be very nice. For example, it could be used with Redux and redux-promise-middleware with no need to wrap original methods (which is really nasty, since they need to be bound to client).

mieubrisse commented 3 years ago

Adding another +1 here; promises would be much, much nicer

GoodMorningA1i commented 3 years ago

+1

The problem faced: Was trying to convert a grpc binding method that had a callback into a promise using util.Promisfy(...), but util.Promisfy(...) was not able to recognize the method signature of my grpc binding method. The reason why this is happening is because the outputted grpc binding method (..._grpc_pb.d.ts) is an overloaded function.

This issue is talked about in further detail here: https://github.com/microsoft/TypeScript/issues/26048

Solution I am following right now as a substitute is to manually construct promises like done here: https://medium.com/expedia-group-tech/the-weird-world-of-grpc-tooling-for-node-js-part-3-d994de02bedc#a-dynamic-client OR here: https://stackoverflow.com/questions/62220154/gprc-client-async-response-nodejs.

agnislav commented 2 years ago

+1. We've got quite complicated logic and currently, there's a choice between callback hell and much more complicated logic spread between a number of functions.

stale[bot] commented 2 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

roboslone commented 2 years ago

It's still relevant.

OnkelTem commented 2 years ago

So what is the current approach? For every generated method make a wrapper?

Goldziher commented 11 months ago

this is really a problem, any progress?

reececomo commented 2 months ago

grpc-web implements a pretty good Promise-based client: grpc_generator.cc#L742

edit: to clarify, just as a point of reference. It uses XMLHttpRequest so not suitable for node.