Skip to content

Instantly share code, notes, and snippets.

@danspinola
Created April 28, 2024 16:18
Show Gist options
  • Save danspinola/29a24fd46e018b724d99e7af17521a3e to your computer and use it in GitHub Desktop.
Save danspinola/29a24fd46e018b724d99e7af17521a3e to your computer and use it in GitHub Desktop.
Breakable `forEach`
function forEach(cb, arr) {
class Break extends Error {};
const _break = _ => { throw new Break(); };
try {
for (const index in arr) {
cb(arr[index], index, arr, _break);
}
}
catch (e) {
if (e instanceof Break) return;
throw e;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment