Skip to content

Instantly share code, notes, and snippets.

View ernestlv's full-sized avatar
Hello World!

Ernest Leyva ernestlv

Hello World!
View GitHub Profile
promise = v => (new Promise(r => setTimeout(x => r(v), ((Math.random() * (10 - 1)) + 1 | 0) * 1000)));
//execs all promises in parallel but prints numbers randomly
[promise(1),promise(2),promise(3),promise(4),promise(5),promise(6)].forEach(function(p) {
return p.then(v => console.log(v));
}, Promise.resolve());
//still execs promises in parallel and uses a sequence to print numbers in order
[promise(1),promise(2),promise(3),promise(4),promise(5),promise(6)].reduce(function(sequence, p) {
@ernestlv
ernestlv / promise2.js
Last active April 24, 2016 17:21
prints 1,2,3,4,5,6 using promises within a sequence
// serialize asynchronous code
// Start off with a promise that always resolves
var sequence = Promise.resolve();
// Loop
[1,2,3,4,5,6].forEach(function(v) {
// Add these actions to the end of the sequence
sequence = sequence.then(function() {
return new Promise(r => setTimeout(x => r(v), ((Math.random() * (10 - 1)) + 1 | 0) * 1000));
}).then(v => console.log(v));
@ernestlv
ernestlv / iterator.js
Last active April 23, 2016 21:40
iterator pattern
function iterate(...args) {
let index = 0;
return {
[Symbol.iterator]() {
return this;
},
next() {
if (index < args.length) {
return { value: args[index++] };
@ernestlv
ernestlv / generator.js
Last active April 24, 2016 16:23
ES6 different generator expressions
var g1 = function* (){ yield 1; yield 2; yield 3; return 'hola'};
var g2 = function* (){ x = yield* g1(); yield 4; return 'bye' }
// yield: 1,2,3,4 ( x becomes 'hola'), bye, done
//else
var g1 = function* (){ yield 1; yield 2; yield 3; return 'hola'};
var g2 = function* (){ yield* g1() };
@ernestlv
ernestlv / promise.js
Last active April 24, 2016 16:21
prints 1,2,3,4,5, 6 using promises at different times
p1 = function(r) {
console.log(new Date(), r);
p2 = new Promise(r => setTimeout(x => r(3), 3000));
p3 = new Promise(r => setTimeout(x => r(4), 7000));
p4 = new Promise(r => setTimeout(x => r(5), 2000));
s = Promise.resolve();
s2 = s.then(x => p2);
@ernestlv
ernestlv / pipe.js
Created April 17, 2016 17:17
Pipes two functions
/*
Pipes two functions
*/
function pipe(fn, fnPipe, obj){
var fnx = function(){};
if (typeof fn === 'function'){
fnx = function(){
var res = fn.apply(this, [].slice.apply(arguments));
return typeof fnPipe === 'function' ? fnPipe.call(this, res) : res;
}.bind(obj || window);
@ernestlv
ernestlv / attempt.js
Last active April 17, 2016 18:23
safe invokes a given function by wraping it in a try/catch, function must be a member of the supplied object. Useful in some contexts like analytic functions in unit tests.
function attempt(obj, p, fnFail){
var fn = function(){};
if (obj && typeof obj[p] === 'function'){
fn = function(){
try {
return this[p].apply(this, [].slice.apply(arguments));
}catch(e){
return typeof fnFail === 'function' ? fnFail(e, p, obj) : console.error('attempt fail:', this['__id__'] || '', p, e);
}
}.bind(obj);
@ernestlv
ernestlv / safeInvoke
Last active April 16, 2016 02:58
wraps the given function p in a try catch so is safer to call it in some contexts. e.g. analytic functions in unit tests.
/*
wraps the given function p in a try catch
so is safer to call it in some contexts. e.g. analytic functions in unit tests.
*/
Object.prototype.try = function (p){
var fn = function(){};
if (typeof this[p] === 'function'){
fn = function(){
try {
this[p].apply(this, [].slice(arguments));
@ernestlv
ernestlv / toArray.js
Created December 16, 2015 18:56
function to convert an Array-Like object to true array
var toArray = Function.prototype.call.bind(Array.prototype.slice);
@ernestlv
ernestlv / staircase.js
Created November 16, 2015 21:58
Stair-case Problem
process.stdin.resume();
process.stdin.setEncoding("ascii");
var input = "";
process.stdin.on("data", function (chunk) {
input += chunk;
});
process.stdin.on("end", function () {
// now we can read/parse input
var n = parseInt(input, 10);
var str = [];