This file contains 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
"use strict"; | |
function isThenable(something) { | |
return ( | |
typeof something === "object" | |
&& something.then instanceof Function | |
); | |
} |
This file contains 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
"use strict"; | |
function serial(tasks, ...args) { | |
let results = []; | |
let prevPromise = Promise.resolve(); | |
for (let curTask of tasks) { | |
prevPromise = prevPromise.then(function(prevResult) { |
This file contains 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
"use strict"; | |
function serial(tasks, ...args) { | |
let results = []; | |
let lastPromise = tasks.reduce(function(prevPromise, curTask) { | |
return prevPromise.then(function(prevResult) { |
This file contains 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
"use strict"; | |
function toPromise(fn, ...args) { | |
// if fn throws an exception, this will reject | |
// the promise with what has been thrown | |
return new Promise(function(resolve) { | |
resolve(fn(...args)); | |
}); |
This file contains 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
"use strict"; | |
function parallel(tasks, ...args) { | |
let error; | |
let results = []; | |
for (let curTask of tasks) { | |
try { | |
results.push(curTask(...args)); |
This file contains 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
// Use Gists to store code you would like to remember later on | |
console.log(window); // log the "window" object to the console |
This file contains 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
// I'm using an IIFE since currently chrome console doesn't allow you to specify let statements in sloppy mode | |
(function test() { | |
"use strict"; | |
let obj = { key1: "value1", key2: "value2" }; | |
let swapped = Object | |
.keys(obj) | |
.reduce((res, key) => (res[obj[key]] = key, res), {}); | |
console.log(swapped); | |
})(); |
This file contains 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
// wrapping into IIAFE to set strict mode, | |
// since otherwise chrome does not allow you to use let/const | |
(() => { | |
"use strict"; | |
let delay = (fn, ms) => (...args) => new Promise(resolve => setTimeout(() => resolve(fn(...args)), ms)); | |
console.time("test"); | |
// Test: | |
// we are delaying 5000 ms an arrow function that returns an array | |
// with "Hello" concatenated with the given args, in this case "world" |
This file contains 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 isPromise(thing) { | |
return ( | |
typeof thing === "object" && thing !== null | |
&& thing.then instanceof Function | |
&& thing.catch instanceof Function | |
); | |
} | |
function whileGenerates(gen, prevGenResult) { |
This file contains 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 recursiveExtend(dest, path) { | |
if (typeof path === "string") { | |
path = path.split("."); | |
} | |
function extend(path, curSegmentIndex, curDest) { | |
if (curSegmentIndex === path.length) { | |
return curDest; | |
} |
OlderNewer