Skip to content

Instantly share code, notes, and snippets.

// 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);
})();
@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
// 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"
@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;
}
@mgtitimoli
mgtitimoli / module.js
Last active January 29, 2016 05:59
common-js compatible module definition
(function(module, exports){
module.exports = {
a: 1,
b: 2
};
}).apply(
undefined,
(
(typeof module === "object" && module !== null)
&& (typeof module.exports === "object" && module.exports !== null)