async primitives challenge
heres some PHP
$p = SM\Promise::Row([# row/line runs concurrently
SM\sleep(1000),
SM\Promise::Func(function(object $r): ?object {
static $N=0;
$N++;
echo '['.$N.']';
return $r->promiseDelay(200);# repeat in 200ms
})
], 0, 1);# stops when: 0=breaks, 1=finishes
echo "> example #1: ";
$t = hrtime(true);
SM\await($p);
echo " in ".((hrtime(true)-$t) / 1000000)."ms\n";
and heres some JS
// NODEJS implementation
const sleep = ms => new Promise(r => setTimeout(r, ms));
async function nums(signal) {
let n = 0;
while (!signal.stop) {
n++;
process.stdout.write('['+n+']');
await sleep(200);
}
}
(async () => {
const signal = { stop: false };
const p = nums(signal);
await sleep(1000);
signal.stop = true;
await p;
})();
first, try to guess the output. second, propose a better variant in your superior language of choice :]