I have some VS Code client that sends a request to a server using:
client.sendRequest(req, cursor).then((answer)=> {
processing...
})
What happens if the server does not (for some good reason) send any answer?
How can I capture this case and show some message consequently?
Assuming you're using https://github.com/microsoft/vscode-languageserver-node/blob/main/client/src/common/client.ts, an educated guess (not verified): Race the returned promise with a promise that resolves after a setTimeout
finishes. Use a signature of sendRequest
where you can pass a cancellation token (you can then use the cancellation token whenever you want. Ex. if promise race resolves with the setTimeout
promise). If the sendRequest promise wins the race, then clearTimeout
the timeout ID from the setTimeout promise.
Code that probably will not work because I didn't actually try it and am just guessing:
const millis = // TODO
let timeoutID;
const cancelTokenSrc = new CancellationTokenSource();
const timeout = new Promise((resolve)=>{timeoutID=setTimeout(resolve,millis,cancelTokenSrc)});
const winner = await Promise.race(timeout, client.sendRequest(/**blah blah, */cancelTokenSrc.token));
if (winner === cancelTokenSrc) {
cancelTokenSrc.cancel();
// timed out.
} else {
clearTimeout(timeoutID);
// do stuff with winner
}