Node.js: Llamadas gRPC y Promesas

Conecta gRPC con Node.js evitando await en callbacks. Convierte getItem a Promise, maneja errores y timeouts, y alinea respuestas HTTP (404, 400, 503).

5 sept 2025 • 3 min read • Q2BSTUDIO Team

Inteligencia-Artificial-

Conectando un servicio gRPC con un backend en Node.js me topé con un detalle que conviene dejar por escrito, porque puede confundir incluso si usas async y await de forma correcta.

Problema

Tienes un controlador que hace await a myService.getItem y luego responde con res.status(200).json(data). Sin embargo, dentro del servicio, getItem invoca this.grpcClient.GetItem usando el patrón clásico de callback. El resultado es que getItem no devuelve una Promise y por tanto el await del controlador no espera nada útil. La secuencia típica de logs es esta: primero aparece controller con undefined y después service con el objeto correcto.

Por qué ocurre

await solo espera Promises o funciones async que devuelven Promises. Si la función interna usa callback, JavaScript no bloquea la ejecución ni sincroniza el flujo. Es equivalente a poner await delante de una operación que no es promesa, como await console.log, que no cambia nada.

Solución directa

Envuelve la llamada gRPC en una nueva Promise. En el callback, si hay error, llama reject con el error; si todo va bien, llama resolve con la respuesta. Así, getItem devuelve una Promise real y el controlador puede esperar correctamente.

Esquema de la solución

Dentro de getItem haz lo siguiente: return new Promise con parámetros resolve y reject. Llama a this.grpcClient.GetItem pasando el payload, por ejemplo item_id item-1234, y el callback. En el callback, si err existe, usa reject err y corta la ejecución. Si no hay err, llama resolve con response. Con esto, en el controlador puedes hacer const data igual await myService.getItem y ya tendrás el objeto final de gRPC en el orden correcto.

Variantes recomendadas

Usar util.promisify cuando el cliente gRPC sigue la convención err primero en el callback. Crear un thin wrapper genérico que promisifique métodos del cliente y estandarice manejo de errores. Añadir deadline y cancelación para evitar llamadas colgadas en redes inestables. Tipar bien la respuesta en TypeScript con una interfaz Item y transformar a tu modelo de dominio antes de devolverla al controlador. Registrar y mapear errores gRPC a códigos HTTP de forma consistente, por ejemplo NOT_FOUND a 404, INVALID_ARGUMENT a 400, UNAVAILABLE a 503.

Lecciones clave

Si una función no devuelve una Promise, await no te ayuda. Adapta siempre APIs basadas en callback a Promises antes de integrarlas en tu capa async. Mantén la separación de responsabilidades: el controlador orquesta y el servicio encapsula detalles de gRPC, mapeo de errores y timeouts.

Cómo te ayuda Q2BSTUDIO

En Q2BSTUDIO diseñamos e implementamos microservicios gRPC y APIs de alto rendimiento como parte de nuestras soluciones de aplicaciones a medida y software a medida, alineadas con requisitos de negocio y escalabilidad en producción. Podemos desplegar y operar estas arquitecturas con observabilidad, seguridad y costes optimizados en la nube con nuestros servicios cloud en AWS y Azure. Nuestro equipo integra inteligencia artificial e ia para empresas, creando agentes IA que automatizan procesos y mejoran la toma de decisiones. Además, reforzamos la ciberseguridad con prácticas de hardening y pentesting, y desplegamos analítica avanzada con servicios inteligencia de negocio y power bi para ofrecer visibilidad inmediata sobre KPIs críticos.

Si buscas un socio tecnológico que una calidad técnica con entrega ágil, descubre nuestro enfoque de desarrollo de aplicaciones y software a medida para acelerar tu roadmap y reducir riesgos en cada iteración.

Palabras clave relacionadas para mejorar tu estrategia digital aplicaciones a medida, software a medida, inteligencia artificial, ia para empresas, agentes IA, ciberseguridad, pentesting, servicios cloud aws y azure, servicios inteligencia de negocio, power bi, automatización de procesos.

A BREAK?

Play for a moment before you go

OUR SERVICES

How we can help you

Artificial intelligence

AI agents, chatbots, and intelligent assistants that automate tasks and serve your customers 24/7 to improve the efficiency of your business.

More info

Software Development

Web, mobile, and desktop applications, intranets, e-commerce, SaaS, and management platforms designed for your company's specific needs.

More info

Cloud services

Migration, infrastructure, managed hosting, high availability, and security on Microsoft Azure and Amazon Web Services to help your business scale without limits.

More info

Cybersecurity and pentesting

Security audits, penetration testing and protection of applications, data and infrastructure on-premise and cloud, with ethical hacking and regulatory compliance.

More info

Business Intelligence

Dashboards and data analysis with Power BI: we integrate your sources, design dashboards and KPIs and turn your data into decisions.

More info

Process automation

We automate repetitive tasks and connect your applications with n8n, Power Automate, Make, and RPA, eliminating manual work and increasing productivity.

More info

Training for Companies

We train your teams in technology with criteria: web development, databases, Git, best practices and security, automation with n8n, artificial intelligence for companies and creation of AI solutions with Azure AI Foundry.

More info

Code Auditing

We audit the code that you, your team or an AI create: we tell you what is good and what to improve, we secure it and make it ready for production, web or app.

More info

AI Image Generation

We create for you the images that your business needs with artificial intelligence: product, networks, advertising, illustration and avatars. You tell us what you want and we deliver it ready to use.

More info

AI Video Generation

We create videos with artificial intelligence for you: promotional, networking, virtual presenters, dubbing and animations. You tell us the idea and we will deliver it assembled and ready to publish.

More info

AI Conversational Avatars

We create conversational avatars with AI – digital humans with a face and voice – that serve your customers and teams with the knowledge of your company, on your website, interactive monitors, WhatsApp or Teams.

More info

Online Marketing and AI

Google Ads, Meta Ads, LinkedIn Ads and AI Engine Positioning (GEO/AEO): we attract customers and make your brand appear where they search for you, also on ChatGPT, Gemini and Perplexity.

More info

Do you have a project in mind?

Tell us your vision and we'll turn it into a software solution. Whatever the scope, we make your idea real.

Live Chat