This file contains 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
// because of the "async precedent" of promises, | |
// expect: a,b,c | |
// get: a,c,b | |
console.log("a"); | |
var p = new Promise(function(resolve,reject){ | |
console.log("c"); | |
}); |
This file contains 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
// If I understand JS grammar and the parsing of it correctly, | |
// `if .. else if ..` is a case of using else without { }, as | |
// it's treated almost the same as `if .. else { if .. }`. | |
// | |
// Those two have *slightly* different parse trees, but the | |
// nesting is clear that `if` statements only have one | |
// consequent and one alternate, so `else if ..` has to be treated | |
// as `else { if ..`. | |
// | |
// Bottom line? If you use `else if`, you're basically committing |
This file contains 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
// With asynquence, if an error occurs in an `or` callback, and you have | |
// no other `or(..)` callbacks registered that could be notified, it's | |
// just thrown as a normal uncaught global error. | |
ASQ() | |
.then(function(){ | |
done("foobar"); | |
}) | |
.or(function(msg){ | |
console.log(msg); // foobar |
This file contains 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
// this works | |
function* foo() { | |
yield 1; | |
yield 1; | |
yield 3; | |
yield 5; | |
yield 8; | |
yield 13; | |
yield 21; |
This file contains 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
if (!JSON.pretty) { | |
JSON.pretty = function jsonpretty(o,f,indent) { | |
f = (f !== undefined) ? f : null; | |
indent = (indent !== undefined) ? indent : "\t"; | |
return JSON.stringify(o,f,indent); | |
}; | |
} |
This file contains 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 normalizeSelector(sel) { | |
// save unmatched text, if any | |
function saveUnmatched() { | |
if (unmatched) { | |
// whitespace needed after combinator? | |
if (tokens.length > 0 && | |
/^[~+>]$/.test(tokens[tokens.length-1]) | |
) { | |
tokens.push(" "); |
This file contains 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* foo(arr){ | |
for (var i=0; i<arr.length; i++) { | |
console.log("foo running: " + i); | |
yield (arr[i] * 2); | |
} | |
} | |
function* bar(arr) { | |
var f = foo(arr); | |
for (var i=0; i<arr.length; i++) { |
This file contains 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
Foo = { baz: 2 /*, .. */ }; | |
// link Bar to Foo | |
Bar = Object.create( Foo ); | |
Bar.bam = 3; | |
// link Foo back to Bar | |
Foo.__proto__ = Bar; | |
// or, in ES6: `Object.setPrototypeOf( Foo, Bar )` |
This file contains 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
class P { | |
foo() { console.log( "P 'foo'" ); } | |
} | |
class C extends P { | |
foo() { | |
super(); | |
} | |
} |
This file contains 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 obj = { | |
_step: 0 | |
}; | |
obj[Symbol.iterator] = function() { | |
return { | |
next: function() { | |
return { value: ++obj._step, done: (obj._step === 4) }; | |
} | |
}; |