Skip to content

Instantly share code, notes, and snippets.

💭
Just coding

Kyle Simpson getify

💭
Just coding
Block or report user

Report or block getify

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@getify
getify / 1.js
Last active Sep 15, 2019
quick sketch of CAF
View 1.js
btn.addEventListener("send-btn",onSend);
send = CAF(send);
var prevSend = Promise.resolve();
function onSend(evt) {
prevSend.finally(function f(){
prevSend = send(
CAF.timeout(1000,"send took too long."),
{ some: "data" }
)
@getify
getify / 1.js
Last active Sep 13, 2019
microtask queue got me singin' the blues
View 1.js
// VERSION 1 -- broken
async function *main() {
yield ready;
}
var resolve1;
var resolve2;
var ready = new Promise(function c(res){
resolve1 = res;
View 1.js
// NOTE: To see this demo: https://codepen.io/getify/pen/LYPbmYG?editors=0012
var counter = 1;
function printMessage() {
console.log(`message ${counter++}`);
}
var schedule = Scheduler(/* debounceMinimum = */50,/* throttleMaximum = */500);
@getify
getify / 1.js
Last active Jul 11, 2019
what could a `replaceAll(..)` for JS strings, which handles non-global regexes as if they're global, look like?
View 1.js
function replaceAll(str,re,replacement) {
if (re.global) {
return str.replace(re,replacement);
}
else {
// do we need to "patch" the replacement function to pass correct arguments?
if (typeof replacement == "function") {
let fn = replacement;
replacement = function replacer(...args) {
// patch `offset` to correct position from whole string
@getify
getify / 1.js
Last active Jun 8, 2019
race-promises-pool
View 1.js
async function racePromisesPool([ ...prs ] = []) {
var raceWon = false;
var prListeners = prs.map(function listen(pr,idx){
return pr.then(function t(v){
if (!raceWon) {
raceWon = true;
prs.splice(idx,1); // remove the promise from the pool since it won the race
return v;
}
});
@getify
getify / 1.js
Created Jun 7, 2019
merging defaults/settings objects with destructuring
View 1.js
ajax(
ajaxOptions({
url: "https://my.other.tld/api",
headers: {
"Cache-Control": "no-cache"
},
cb: resp => console.log(resp)
})
);
@getify
getify / 1.js
Last active May 31, 2019
Force Monad #MayThe4thBeWithYou
View 1.js
var Force = { Skywalker, RegularFolk, of: Skywalker };
function Skywalker(v) {
return { map, chain, ap };
function map(fn) {
return Skywalker(fn(v));
}
function chain(fn) {
return fn(v);
}
@getify
getify / 1.js
Last active Jun 3, 2019
is Maybe a "monad?
View 1.js
// is Just(..) a monad? Well, it's a monad constructor.
// Its instances are certainly monads.
function Just(v) {
return { map, chain, ap };
function map(fn) {
return Just(fn(v));
}
function chain(fn) {
return fn(v);
}
@getify
getify / 1.js
Created Apr 9, 2019
if..else if.. else clauses
View 1.js
// note: mathematically, `x` can only ever be 0, 1, 2, or 3
var x = someNumber % 4;
// let's consider some options for an if..else if..else clause series...
@getify
getify / 1.js
Last active Apr 11, 2019
Nested ternary: good vs bad
View 1.js
var data = x ? y ? z ? z : y : x : 0;
// I call this the "if..if" pattern, and it's BAD. It means:
var data;
if (x) {
if (y) {
if (z) {
data = z;
}
You can’t perform that action at this time.