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
| // VERSION 1 -- broken | |
| async function *main() { | |
| yield ready; | |
| } | |
| var resolve1; | |
| var resolve2; | |
| var ready = new Promise(function c(res){ | |
| resolve1 = res; |
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
| // 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); |
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 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 |
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
| 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; | |
| } | |
| }); |
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
| ajax( | |
| ajaxOptions({ | |
| url: "https://my.other.tld/api", | |
| headers: { | |
| "Cache-Control": "no-cache" | |
| }, | |
| cb: resp => console.log(resp) | |
| }) | |
| ); |
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
| 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); | |
| } |
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
| // 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); | |
| } |
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
| // 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... |
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
| 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; | |
| } |
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
| // these `xx` parameters are all used, so no "unused parameter" errors should be reported | |
| var f; | |
| f = (xx) => xx; | |
| f = (xx) => () => xx; | |
| f = (xx) => { xx; }; | |
| f = (xx, yy = xx) => { yy; }; | |
| f = (xx, yy = xx) => { var xx; xx = yy; }; | |
| f = (xx, yy = xx) => { var xx = yy; }; |