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
@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 / 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 / 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 / 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 / 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 / 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));
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 / promise4.js
Last active April 27, 2016 22:56
ES7: Executes functions asynchronously using promises and generators.
//invokes segments of an asynchronus function
function asyncF(generator) {
try {
var steps = generator(); // function to exec by segments
return iterate(steps.next());
} catch(e) {
return Promise.reject(e); // rejects iteration
}
function iterate({value, done}) { // arg has value of prev iteration
if (done) return Promise.resolve(value); // resolves all pending promises
@ernestlv
ernestlv / id.js
Created April 26, 2016 15:10
generates an ID
var id = (Math.random() * (1000000000 - 1) + 1 | 0) + ":" + (new Date().getTime())
@ernestlv
ernestlv / timer.html
Last active November 4, 2016 16:30
Timer written in ReactJS
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>React Timer</title>
<script src="https://unpkg.com/[email protected]/dist/react.js"></script>
<script src="https://unpkg.com/[email protected]/dist/react-dom.js"></script>
<script src="https://unpkg.com/[email protected]/babel.min.js"></script>
</head>
<body>