Hi Jun, Thanks for reaching out!
My understanding is that it might not be possible to achieve what you want with the current grpc-web library. The reason is that when you call *stream.cancel()*, you're essentially cancelling the underlying server request (e.g. XHR), which is cutting off this chain of response as indicated in the diagram below: [image: grpc-web-interceptors.jpg] (original image: https://grpc.io/blog/grpc-web-interceptor/#binding-interceptors) I wonder if it'd be possible to implement caching one layer above the grpc-web library — since in this case it doesn't seem like you necessarily need to make this RPC at all? Thanks! On Sunday, August 27, 2023 at 6:41:03 AM UTC-7 Jun wrote: > I'm trying to return a cached response through the interceptor, but I > couldn't find a way to make an early return. When the call is cancelled in > the interceptor, the callback function doesn't get run. I get neither the > error nor the response. Ideally, I would like to return a cached response > after the call is cancelled. > > Here's the interceptor. I called the cancel method inside: > > ```js > const StreamInterceptor = function () {}; > StreamInterceptor.prototype.intercept = function (request, invoker) { > const InterceptedStream = function (stream) { > this.stream = stream; > }; > InterceptedStream.prototype.on = function (eventType, callback) { > this.stream.on(eventType, callback); > return this; > }; > const stream = invoker(request); > stream.cancel(); > return new InterceptedStream(stream); > }; > ``` > > Here's the client and the method call. The call is unary. > > ```js > const client = new ServiceClient('http://localhost:8080', null, { > streamInterceptors: [new StreamInterceptor()], > }); > client.hello(message, {}, (err, res) => { > console.log(res); > }); > ``` > -- You received this message because you are subscribed to the Google Groups "grpc.io" group. To unsubscribe from this group and stop receiving emails from it, send an email to grpc-io+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/grpc-io/b85f0e80-f19a-415f-bd65-bc80fdfb10efn%40googlegroups.com.