Created
April 28, 2024 16:18
-
-
Save danspinola/29a24fd46e018b724d99e7af17521a3e to your computer and use it in GitHub Desktop.
Breakable `forEach`
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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