Skip to content

Instantly share code, notes, and snippets.

View danspinola's full-sized avatar

Daniel Spinola danspinola

  • IBM
View GitHub Profile
@danspinola
danspinola / enumerate.js
Created April 29, 2024 12:30
Pythonic Enumerate
function* enumerate(arr) {
for (const index in arr) {
yield [index, arr[index]];
}
}
@danspinola
danspinola / context-manager.js
Last active April 28, 2024 16:39
Context Manager
/**
* @params manager {{
source: S,
open(source: S): R,
close(resource: R): void,
}} - An object with a source, and two functions used to connect and clean-up the resource.
*/
function using(manager, cb) {
const resource = manager.open(manager.source);
try {
@danspinola
danspinola / breakable-foreach.js
Created April 28, 2024 16:18
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;
@danspinola
danspinola / promise-state.js
Last active April 28, 2024 16:15
JS: Check the state of a Promise
function yet(promise) {
const states = Object.freeze({
PENDING: "pending",
FULFILLED: "filfilled",
REJECTED: "rejected"
});
const yettable = {
state: "pending",
_result: undefined,