let microtaskSet = new Set();
function addMicrotask(func) {
if (typeof func !== 'function') return;
if (!microtaskSet.size) {
// delayed execution callback after updating store
globalThis.queueMicrotask(() => {
const set = microtaskSet
microtaskSet = new Set();
set .forEach((f) => f());
});
}
microtaskSet.delete(func);
microtaskSet.add(func);
}
addMicrotask(() => { throw new Error() })
addMicrotask(() => console.log(1))