Skip to content

Instantly share code, notes, and snippets.

@mgtitimoli
Last active September 5, 2015 19:33
Show Gist options
  • Save mgtitimoli/4d923f6952cb33942856 to your computer and use it in GitHub Desktop.
Save mgtitimoli/4d923f6952cb33942856 to your computer and use it in GitHub Desktop.
// 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"
// and after executing this arrow function we are passing its result ([ "Hello", "world" ])
// to another arrow function, which uses the comma operator to execute 2 expressions without
// needing to wrap it with curly braces, where one of them logs the amount of time the test take to run,
// and the other the arguments received, that are the ones fn returned when it was used to set
// the promise resolution value, therefore [ "Hello", "World" ], and we are using the spread operator
// so it will end up printing "Hello World" after 5000 ms
// (3 lines of code, 9 lines of comments :P)
delay((...args) => [ "Hello", ...args ], 5000)("World")
.then(args => (console.timeEnd("test"), console.log(...args)));
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment