Skip to content

Instantly share code, notes, and snippets.

View ericelliott's full-sized avatar
💭
https://leanpub.com/composingsoftware

Eric Elliott ericelliott

💭
https://leanpub.com/composingsoftware
View GitHub Profile
@ericelliott
ericelliott / class-inheritance-demo.js
Last active December 22, 2020 17:07
Class inheritance demo (not recommended)
class User {
constructor ({ name = 'Anonymous' }) {
this.name = name;
}
login () {
console.log(`${ this.name } logged in.`);
}
};
class Student extends User {
@ericelliott
ericelliott / filter.js
Last active December 22, 2020 17:19
Filter implemented with reduce
const filter = (fn, arr) => arr.reduce((newArr, item) => {
return fn(item) ? newArr.concat([item]) : newArr;
}, []);
@ericelliott
ericelliott / autocurry.js
Created January 28, 2017 05:35
Autocurry
const curry = fn => (...args1) => {
if (args1.length === fn.length) {
return fn(...args1);
}
return (...args2) => {
const args = [...args1, ...args2];
if (args.length >= fn.length) {
return fn(...args);
@ericelliott
ericelliott / wait-speculation.js
Created January 23, 2017 02:51
Wait with speculation
import speculation from 'speculation';
const wait = (
time,
cancel = Promise.reject() // By default, don't cancel
) => speculation((resolve, reject, onCancel) => {
const timer = setTimeout(resolve, time);
// Use onCancel to clean up any lingering resources
// and then call reject(). You can pass a custom reason.
@ericelliott
ericelliott / speculation.js
Created January 23, 2017 02:45
Speculation: Cancellable promise implementation. Maintained production version here: https://github.com/ericelliott/speculation
// HOF Wraps the native Promise API
// to add take a shouldCancel promise and add
// an onCancel() callback.
const speculation = (
fn,
cancel = Promise.reject() // Don't cancel by default
) => new Promise((resolve, reject) => {
const noop = () => {};
const onCancel = (
@ericelliott
ericelliott / promise-chaining.js
Last active April 3, 2020 02:13
Promise chaining behaviors
const wait = time => new Promise(
res => setTimeout(() => res(), time)
);
wait(200)
// onFulfilled() can return a new promise, `x`
.then(() => new Promise(res => res('foo')))
// the next promise will assume the state of `x`
.then(a => a)
// Above we returned the unwrapped value of `x`
@ericelliott
ericelliott / cancellable-wait.js
Last active October 8, 2019 08:06
Cancellable wait -- an ES6 promise example
const wait = (
time,
cancel = Promise.reject()
) => new Promise((resolve, reject) => {
const timer = setTimeout(resolve, time);
const noop = () => {};
cancel.then(() => {
clearTimeout(timer);
reject(new Error('Cancelled'));
@ericelliott
ericelliott / wait.js
Created January 19, 2017 22:16
Wait -- an ES6 promise example
const wait = time => new Promise((resolve) => setTimeout(resolve, time));
wait(3000).then(() => console.log('Hello!')); // 'Hello!'
@ericelliott
ericelliott / map-custom-data-type.js
Created January 4, 2017 00:20
Map with custom data type
const double = n => n.points * 2;
const doubleMap = numbers => numbers.map(double);
console.log(doubleMap([
{ name: 'ball', points: 2 },
{ name: 'coin', points: 3 },
{ name: 'candy', points: 4}
])); // [ 4, 6, 8 ]
@ericelliott
ericelliott / double-mapping.js
Created January 4, 2017 00:14
Double mapping
const double = n => n * 2;
const doubleMap = numbers => numbers.map(double);
console.log(doubleMap([2, 3, 4])); // [ 4, 6, 8 ]