getPromiseSettledAnytimes
code:mod.ts
export function getPromiseSettledAnytimes<T, E = unknown>() {
let _resolve: ((value: T) => void) | undefined;
let _reject: ((value: E) => void) | undefined;
const waitForSettled = () => new Promise<T>(
(res, rej) => {
_resolve = res;
_reject = rej;
},
);
const resolve = (value: T) => _resolve?.(value);
const reject = (reason: E) => _reject?.(reason);
}
Async Generatorを使わずとも、これでevent listenerをwrapしてwhile(true) {...}で呼び出せば同じことを実現できるんじゃないか? code:ts
addEventListener("mousemove", resolve);
addEventListener("mouseup", resolve);
while (true) {
const moveEvent = await waitMove();
if (moveEvent.type === "mouseup") break;
// ...
}
removeEventListener("mousemove", resolve);
removeEventListener("mouseup", resolve);
一周回って戻ってきた