This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function iterate(...args) { | |
let index = 0; | |
return { | |
[Symbol.iterator]() { | |
return this; | |
}, | |
next() { | |
if (index < args.length) { | |
return { value: args[index++] }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var id = (Math.random() * (1000000000 - 1) + 1 | 0) + ":" + (new Date().getTime()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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> |