0

I'm trying to call a simple grpc request in a function but I don't know how to wait for the response and pass it outside the grpc call.

It should be something like that:

Client: (parent, args, context, info) =>{
      client.Get({thing, objects, properties}, function(err, response) {
            //how to return the response outside?
          });
      //await for the response function.
};

Is it possible to return the response in some way outside the client.Get function? Does someone have some suggestions? Thank you in advice!

1 Answer 1

10

You have two ways:

  1. Wrap the methods of gRPC from error first callback to promise by yourself.
new Promise((resolve, reject) => client.Get({thing, objects, properties}, function(err, response) {
  if(err) {
    return reject(err)
  }
  resolve(response)        
}))
  1. Use grpc-promise to promisify the methods of gRPC client.
const grpc_promise = require('grpc-promise');;
grpc_promise.promisifyAll(client);

Then, you can pass this promisified gRPC client to GraphQL context. (Guess you are using GraphQL based on the function signature).

Then you can return a promise in a GraphQL resolver.

Client: (parent, args, context, info) =>{
  return context.client.Get({thing, objects, properties})
};
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.