Skip to content

Instantly share code, notes, and snippets.

Generators vs Fibers

Both ES6 generators and node-fibers can be used to block a coroutine while waiting on some I/O without blocking the entire process. Both can do this for arbitrarily deep call stacks. The main difference between the capabilities of the two is how explicit the syntax is.

Generators - Safe, but Explicit

In code that uses ES6 generators:

var run = require('gen-run'); // https://github.com/creationix/gen-run
global
daemon
defaults
mode http
timeout connect 86400000
timeout server 86400000
timeout client 86400000
timeout check 5s
javascript:( function() {
console.group( 'Performance Information for all entries of ' + window.location.href );
console.log( '\n-> Duration is displayed in ms\n ' )
var entries = window.performance.getEntries();
entries = entries.sort( function( a, b ) {
return b.duration - a.duration;
} );
var OAuth2 = require('OAuth').OAuth2;
var https = require('https');
var oauth2 = new OAuth2(KEY, SECRET, 'https://api.twitter.com/', null, 'oauth2/token', null);
oauth2.getOAuthAccessToken('', {
'grant_type': 'client_credentials'
}, function (e, access_token) {
console.log(access_token); //string that we can use to authenticate request
var options = {
<ul>
<li ng-repeat="item in items | orderObjectBy:'color':true">{{ item.color }}</li>
</ul>
@iksose
iksose / uri.js
Created August 24, 2014 15:39 — forked from jlong/uri.js
var parser = document.createElement('a');
parser.href = "http://example.com:3000/pathname/?search=test#hash";
parser.protocol; // => "http:"
parser.hostname; // => "example.com"
parser.port; // => "3000"
parser.pathname; // => "/pathname/"
parser.search; // => "?search=test"
parser.hash; // => "#hash"
parser.host; // => "example.com:3000"
@iksose
iksose / jsbeautifier-es6-hacks
Last active January 28, 2017 15:35 — forked from loopmode/jsbeautifier-es6-hacks
Some (ugly!) hacks to fix es6/es7 formatting problems in jsbeautifier. I use it with the "html/css/js prettify" plugin for Sublime Text
// in Sublime Text, Preferences -> Browse Packages, find HTML-CSS-JS Prettify/scripts/node_modules/js-beautify/js/lib/beautify.js
// find the last line of the 'beautify' function, and place the next lines just before 'return sweet_code';
// https://gist.github.com/loopmode/d091bce3b76efaf15d63
// es7 decorators
sweet_code = sweet_code.replace(/@\n/g, '@');
sweet_code = sweet_code.replace(/\)@\s/g, ')\n@');
sweet_code = sweet_code.replace(/ @ /g, ' @');
sweet_code = sweet_code.replace(/@\s/g, '\n@');
// destructuring/spread: imports