oh, I assumed you were using Go.
not a problem, though. even though the JS demo is implemented with lin-kv
(https://github.com/jepsen-io/maelstrom/blob/main/demo/js/single_key_txn.js) I think their problem statement is a bit different than what fly.io asks todo.
Therefore, for solving fly.io challenge in JS it seems like you still can do it with the following trick (I am sorry, I am not writing node.js for many many years, but that worked for me) - use async function generators as in async function - JavaScript | MDN* like that:
/* jshint esversion: 9 */
// A basic echo server
var node = require('./node');
function timeout(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
async function* tx_step(j) {
i = 0;
while (i < 100) {
i ++;
await timeout(50);
yield await Promise.resolve(j + i);
}
}
async function process_tx(id) {
for await (const val of tx_step(id)) {
console.error(val);
}
}
id = 1;
node.on('echo', function(req) {
process_tx(id);
node.reply(req, {type: 'echo_ok', 'echo': req.body.echo});
id *= 10;
});
node.main();
execute like
~/maelstrom/maelstrom test -w echo --bin ./1.js --node-count 1 --time-limit 10 --concurrency 2 --rate 2 --log-stderr
and you will see that it does the trick:
INFO [2023-04-04 10:44:08,430] jepsen worker 0 - jepsen.util 0 :invoke :echo "Please echo 96"
INFO [2023-04-04 10:44:08,431] n0 stderr - maelstrom.process Got {"id":2,"src":"c2","dest":"n0","body":{"echo":"Please echo 96","type":"echo","msg_id":1}}
INFO [2023-04-04 10:44:08,432] n0 stderr - maelstrom.process Sending {
INFO [2023-04-04 10:44:08,432] n0 stderr - maelstrom.process src: 'n0',
INFO [2023-04-04 10:44:08,432] n0 stderr - maelstrom.process dest: 'c2',
INFO [2023-04-04 10:44:08,432] n0 stderr - maelstrom.process body: { type: 'echo_ok', echo: 'Please echo 96', in_reply_to: 1 }
INFO [2023-04-04 10:44:08,432] n0 stderr - maelstrom.process }
INFO [2023-04-04 10:44:08,435] jepsen worker 0 - jepsen.util 0 :ok :echo {:type "echo_ok", :echo "Please echo 96", :in_reply_to 1}
INFO [2023-04-04 10:44:08,482] n0 stderr - maelstrom.process 2
INFO [2023-04-04 10:44:08,532] n0 stderr - maelstrom.process 3
INFO [2023-04-04 10:44:08,537] jepsen worker 0 - jepsen.util 0 :invoke :echo "Please echo 68"
INFO [2023-04-04 10:44:08,538] n0 stderr - maelstrom.process Got {"id":4,"src":"c2","dest":"n0","body":{"echo":"Please echo 68","type":"echo","msg_id":2}}
INFO [2023-04-04 10:44:08,538] n0 stderr - maelstrom.process Sending {
INFO [2023-04-04 10:44:08,538] n0 stderr - maelstrom.process src: 'n0',
INFO [2023-04-04 10:44:08,538] n0 stderr - maelstrom.process dest: 'c2',
INFO [2023-04-04 10:44:08,538] n0 stderr - maelstrom.process body: { type: 'echo_ok', echo: 'Please echo 68', in_reply_to: 2 }
INFO [2023-04-04 10:44:08,538] n0 stderr - maelstrom.process }
INFO [2023-04-04 10:44:08,539] jepsen worker 0 - jepsen.util 0 :ok :echo {:type "echo_ok", :echo "Please echo 68", :in_reply_to 2}
INFO [2023-04-04 10:44:08,583] n0 stderr - maelstrom.process 2
INFO [2023-04-04 10:44:08,589] n0 stderr - maelstrom.process 12
INFO [2023-04-04 10:44:08,632] n0 stderr - maelstrom.process 4
INFO [2023-04-04 10:44:08,640] n0 stderr - maelstrom.process 14
INFO [2023-04-04 10:44:08,683] n0 stderr - maelstrom.process 6
INFO [2023-04-04 10:44:08,691] n0 stderr - maelstrom.process 16
INFO [2023-04-04 10:44:08,733] n0 stderr - maelstrom.process 8
INFO [2023-04-04 10:44:08,743] n0 stderr - maelstrom.process 18
execution interleaves…
even
async function* tx_step(j) {
...
while (i < 50) {
...
await timeout(10);
...
}
}
node.on('echo', async function(req) {
...
await process_tx(id);
...
});
INFO [2023-04-04 10:57:54,127] jepsen worker 1 - jepsen.util 1 :invoke :echo "Please echo 26"
INFO [2023-04-04 10:57:54,129] n0 stderr - maelstrom.process Got {"id":4,"src":"c2","dest":"n0","body":{"echo":"Please echo 26","type":"echo","msg_id":2}}
INFO [2023-04-04 10:57:54,139] n0 stderr - maelstrom.process 101
...
INFO [2023-04-04 10:57:54,253] n0 stderr - maelstrom.process 112
INFO [2023-04-04 10:57:54,263] jepsen worker 0 - jepsen.util 0 :invoke :echo "Please echo 62"
INFO [2023-04-04 10:57:54,263] n0 stderr - maelstrom.process 113
INFO [2023-04-04 10:57:54,266] n0 stderr - maelstrom.process Got {"id":5,"src":"c3","dest":"n0","body":{"echo":"Please echo 62","type":"echo","msg_id":1}}
INFO [2023-04-04 10:57:54,274] n0 stderr - maelstrom.process 101
INFO [2023-04-04 10:57:54,277] n0 stderr - maelstrom.process 1002
INFO [2023-04-04 10:57:54,285] n0 stderr - maelstrom.process 103
INFO [2023-04-04 10:57:54,287] n0 stderr - maelstrom.process 1004
INFO [2023-04-04 10:57:54,295] n0 stderr - maelstrom.process 105