In Rust there are relatively new features, async and await.
This syntax is a godsend but I am not sure how to use it in an evented i/o pattern in Rust.
One such example of a use is where there are websockets and you send a message and are waiting for a particular message to return, but there is just one onMessage and one sendMessage for the entire application.
So I have tried to make a Javascript equivalent where I use a setInterval to run a clock (to simulate multiple onMessage events in the case of websockets.
How would I write this Javascript in Rust/or Tokio?. Again I'm not actually going for this code but going for the conceptual sendMessage to a websocket server such that the method call waits a corresponding answer.
let setAlarm = (seconds) => {
return new Promise((resolve, reject) {
if(seconds > 60 || seconds < 0) {
return reject(new Error("Invalid seconds"));
}
//Represents going through many messages
//waiting for the matching id indicating
//its a reply to my original message
let handle = setInterval(function() {
let date = new Date();
if(date.getSeconds() == seconds) {
clearInterval(handle);
resolve(date);
}
}, 1000);
});
}
async function main() {
//Wait for 45th second
await setAlam(45);
console.log("Running tasks now");
}