Skip to content

Instantly share code, notes, and snippets.

@spotsccc
Last active June 23, 2025 20:33
Show Gist options
  • Save spotsccc/ba08076f6489ebbde8cf6c14e6b1d99a to your computer and use it in GitHub Desktop.
Save spotsccc/ba08076f6489ebbde8cf6c14e6b1d99a to your computer and use it in GitHub Desktop.
import { context, Fn, noop, top, wrap } from '@reatom/core';
function takeNested(cb: Fn) {
const frame = top();
return new Promise<void>((resolve, reject) => {
let i = 1;
const check = () => {
if (i === 0) {
resolve();
}
if (--i === 0) Promise.resolve().then(check);
};
const { pushQueue } = frame.root;
frame.root.pushQueue = (cb: Fn, queue) => {
pushQueue.call(
frame.root,
() => {
const result = cb();
if (result instanceof Promise) {
++i;
result.finally(check).catch(noop);
}
return result;
},
queue,
);
};
const result = frame.run(cb);
check();
return result;
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment