Skip to content

Instantly share code, notes, and snippets.

@mgtitimoli
mgtitimoli / gist:a158ee257adf6753f652
Last active August 29, 2015 14:24
Promise based serial task executor (version #1 - Only creates promises if any task returns one)
"use strict";
function isThenable(something) {
return (
typeof something === "object"
&& something.then instanceof Function
);
}
@mgtitimoli
mgtitimoli / gist:819dd223a7075a6201dd
Last active August 29, 2015 14:24
Promise based serial tasks executor (version #2: Create initial promise | for of)
"use strict";
function serial(tasks, ...args) {
let results = [];
let prevPromise = Promise.resolve();
for (let curTask of tasks) {
prevPromise = prevPromise.then(function(prevResult) {
@mgtitimoli
mgtitimoli / gist:2f0206d82ae85e85404f
Last active August 29, 2015 14:24
Promise based serial tasks executor (version #3: Create initial promise | Array.prototype.reduce)
"use strict";
function serial(tasks, ...args) {
let results = [];
let lastPromise = tasks.reduce(function(prevPromise, curTask) {
return prevPromise.then(function(prevResult) {
@mgtitimoli
mgtitimoli / gist:901bd6935acdfacd89a6
Last active August 29, 2015 14:24
to-promise: convert given callback into a promise
"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));
});
@mgtitimoli
mgtitimoli / gist:d7a9b94ab64a21f4cb57
Last active August 29, 2015 14:24
Promise based parallel task executor
"use strict";
function parallel(tasks, ...args) {
let error;
let results = [];
for (let curTask of tasks) {
try {
results.push(curTask(...args));
@mgtitimoli
mgtitimoli / 0_reuse_code.js
Last active August 29, 2015 14:25
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console
// 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);
})();
// 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"
function isPromise(thing) {
return (
typeof thing === "object" && thing !== null
&& thing.then instanceof Function
&& thing.catch instanceof Function
);
}
function whileGenerates(gen, prevGenResult) {
@mgtitimoli
mgtitimoli / extend-recursive.js
Last active January 25, 2016 16:23
Module extend
function recursiveExtend(dest, path) {
if (typeof path === "string") {
path = path.split(".");
}
function extend(path, curSegmentIndex, curDest) {
if (curSegmentIndex === path.length) {
return curDest;
}