Skip to content

Instantly share code, notes, and snippets.

View getify's full-sized avatar
💭
Just coding

Kyle Simpson getify

💭
Just coding
View GitHub Profile
@getify
getify / gist:9124710
Created February 20, 2014 22:34
sync, not async
// 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");
});
@getify
getify / gist:9179137
Created February 23, 2014 23:56
if..else if..else --> you used block-less else!
// 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
@getify
getify / gist:9309531
Created March 2, 2014 16:55
no such thing as "silent errors" in asynquence, like there is with other promises' libs. So, asynquence doesn't need the hack of `done(..)` like others do.
// 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
@getify
getify / gist:9335735
Last active August 29, 2015 13:56
could generators have a default iterator?
// this works
function* foo() {
yield 1;
yield 1;
yield 3;
yield 5;
yield 8;
yield 13;
yield 21;
@getify
getify / gist:9403543
Created March 7, 2014 01:51
JSON.pretty
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);
};
}
@getify
getify / normalize.js
Last active August 29, 2015 13:57
normalizing CSS selectors (consolidating/inserting whitespace, removing comments, etc)
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(" ");
@getify
getify / gist:9816833
Last active August 29, 2015 13:57
exploring my confusion of `yield*`
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++) {
@getify
getify / gist:9822436
Created March 28, 2014 00:32
cyclic __proto__ ... disallowed?
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 )`
@getify
getify / gist:9936912
Last active August 29, 2015 13:58
super re-binding?
class P {
foo() { console.log( "P 'foo'" ); }
}
class C extends P {
foo() {
super();
}
}
@getify
getify / gist:10034276
Created April 7, 2014 19:19
bug with for-of iteration of traceur/es6fiddle?
var obj = {
_step: 0
};
obj[Symbol.iterator] = function() {
return {
next: function() {
return { value: ++obj._step, done: (obj._step === 4) };
}
};