Skip to content

Instantly share code, notes, and snippets.

View whitelizard's full-sized avatar
💚
Focusing

Esbjörn Blomquist whitelizard

💚
Focusing
View GitHub Profile

Keybase proof

I hereby claim:

  • I am whitelizard on github.
  • I am whitelizard (https://keybase.io/whitelizard) on keybase.
  • I have a public key ASDQQj8ThUyAUGANASXBwYlA-tT0kJ4sMe5qFwRZSvs8-Ao

To claim this, I am signing this object:

@whitelizard
whitelizard / promise-race-test-1.js
Last active November 6, 2021 16:35
Promise.race Test 1
describe('publish/subscribe 1', () => {
it('should send a message from publish to subscribe', async () => {
let result;
subscribe('ch1', (msg) => (result = msg));
publish('ch1', 1);
await wait(900);
expect(result).toBe(1);
});
});
@whitelizard
whitelizard / promise-race-util-1.js
Last active October 30, 2019 09:18
Promise.race Util 1
const triggerPromise = () => {
let resolve;
const promise = new Promise(r => (resolve = r));
return [promise, resolve];
};
@whitelizard
whitelizard / promise-race-test-2.js
Last active November 6, 2021 16:36
Promise.race Test 2
describe('publish/subscribe 2', () => {
it('should send a message from publish to subscribe', async () => {
let result;
const [promise, resolve] = triggerPromise();
subscribe('ch1', (msg) => {
result = msg;
resolve();
});
publish('ch1', 1);
await promise;
@whitelizard
whitelizard / promise-race-util-2.js
Last active November 6, 2021 16:37
Promise.race Util 2
const asyncWithTimeout = (promise, timeout = 900) =>
Promise.race([
promise,
new Promise((_, reject) =>
setTimeout(() => reject(Error('Async timeout')), timeout)
),
]);
@whitelizard
whitelizard / promise-race-test-3.js
Last active November 6, 2021 16:38
Promise.race Test 3
describe('publish/subscribe 3', () => {
it('should send a message from publish to subscribe', async () => {
let result;
const [promise, resolve] = triggerPromise();
subscribe('ch1', (msg) => {
result = msg;
resolve();
});
publish('ch1', 1);
await asyncWithTimeout(promise);
@whitelizard
whitelizard / safer-code-es6-destructuring-1.js
Created October 30, 2019 12:56
Safer Code ES6 Destructuring 1
// item and items are predefined
const parent = items[item.parent];
const types = parent.types;
const name = parent.name;
@whitelizard
whitelizard / safer-code-es6-destructuring-2.js
Created October 30, 2019 12:57
Safer Code ES6 Destructuring 2
const parentId = item.parent;
const parent = parentId ? (items[parentId] || {}) : {};
const types = parent.types || [];
const name = parent.name || 'Noname';
@whitelizard
whitelizard / safer-code-es6-destructuring-3.js
Created October 30, 2019 12:57
Safer Code ES6 Destructuring 3
const { parent: parentId } = item;
const { [parentId]: { types = [], name = 'Noname' } = {} } = items;
@whitelizard
whitelizard / oop-to-fp-class.js
Created November 18, 2019 23:39
Traditional OOP style class in JavaScript
class Circle {
constructor(pos = [0, 0], r = 0) {
this.pos = pos;
this.radius = r;
this.hitCount = 0;
}
get r() {
return this.radius;
}