Replies: 1 comment 7 replies
-
Not promise based, but you could use something like this: const withTimeout = (onSuccess, onTimeout, timeout) => {
let called = false;
const timer = setTimeout(() => {
if (called) return;
called = true;
onTimeout();
}, timeout);
return (...args) => {
if (called) return;
called = true;
clearTimeout(timer);
onSuccess.apply(this, args);
}
}
socket.emit("hello", 1, 2, withTimeout(() => {
console.log("success!");
}, () => {
console.log("timeout!");
}, 1000)); Reference: https://socket.io/docs/v4/emitting-events/#acknowledgements |
Beta Was this translation helpful? Give feedback.
7 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I am using the typed event and thus res is inferred but it does not infer the type of the promise. So the Promise is typed Promise although the type returnData is inferred for the res.
This solution does not infer types which is suboptimal but also writing this everytime is alot of overhead.
It would be nice to just call it like below and get a promise with the correct return type (with optional timeout).
Do you have any better way to write this?
Beta Was this translation helpful? Give feedback.
All reactions