Skip to content

Instantly share code, notes, and snippets.

/**
* The Article domain object, this can be completely custom or
* part of some ORM. It doesn't really matter. It's an object you
* can call methods on. As long as you return some compatible
* promise-like thing to the data/render cycle
*/
var util = require('util')
, Q = require('q') // https://github.com/kriskowal/q
, db = require('./data-layer')
, Domain = require('./domain');
@polotek
polotek / route.js
Created December 6, 2011 18:50 — forked from creationix/route.js
Sanity check for async template idea
Creationix.route("GET", "/", function (req, res, params, next) {
render("frontindex", {
title: query("index", "title"),
links: query("index", "links"),
articles: loadArticles
}, function (err, html) {
if (err) return next(err);
res.writeHead(200, {
"Content-Length": Buffer.byteLength(html),
"Content-Type": "text/html; charset=utf-8"
@polotek
polotek / route.js
Created December 5, 2011 19:19 — forked from creationix/route.js
Sanity check for async template idea
Creationix.route("GET", "/", function (req, res, params, next) {
loadIndex(function (err, home) {
home.render("frontindex",
links: query("index", "links"),
articles: loadArticles()
}, function (err, html) {
if (err) return next(err);
res.writeHead(200, {
"Content-Length": Buffer.byteLength(html),
"Content-Type": "text/html; charset=utf-8"
@polotek
polotek / sunshine_democracy.md
Created November 14, 2011 04:14
Snippet of dialog from the movie sunshine. http://en.wikiquote.org/wiki/Sunshine_(2007_film)

Searle: It would, of course, be absurd to alter our trajectory to assist the crew of the Icarus I. Even if we knew that some or even all of that crew are still alive, their lives are entirely expendable when seen in the context of our mission. As are our own lives.

Mace: Exactly.

Searle: However, there is something on board the Icarus I that may be worth the detour. As you pointed out, Mace, we have a payload to deliver. A payload, singular. Now, everything about the delivery and effectiveness of that payload is entirely theoretical. Simply put, we don't know if it's gonna work. But what we do know is this: if we had two bombs, we'd have two chances.

Kaneda: You're assuming we'd be able to pilot Icarus I.

Searle: Yes.

var cursor = dbclient.collection('example', _).find(_);
var count = cursor.count(_);
for(var obj = cursor.nextObject(_), oi = 0; obj; obj =
cursor.nextObject(_), oi++) {
/**
* do something with every obj here
* suppose for this example it needs total count and offset (oi).
*/
});
// This takes a long time to respond and we don't want to call it every time.
var deferred = Deferred();
exports.getData = function() {
if(deferred.isResolved()) {
// Make a new call
deferred = Deferred();
_getDataFromCrappyService(deferred.resolve);
}
return deferred.promise;
@polotek
polotek / clone.js
Created September 24, 2011 19:22 — forked from rwaldron/clone.js
Real, deep copied objects.
function hasOwnProperty(key) {
if(this[key]) {
var proto = this.prototype;
if(proto) {
return ((key in this.prototype) && (this[key] === this.prototype[key]));
}
return true;
} else {
return false;
}
var httpclient = require('httpclient');
exports.app = function() {
return {
status : 200,
headers : {},
body: new httpclient
.HttpClient({
url: 'http://google.com'
}).finish().body
};
#!/usr/bin/env node
require('long-stack-traces');
var urlUtils = require('url')
, util = require('util')
, nopt = require('nopt')
, EventEmitter = require('events').EventEmitter
, _ = require('underscore')
, socketio = require('socket.io-client');
var Benchmark = require('benchmark');
var suite = new Benchmark.Suite();
var EventEmitter = require('events').EventEmitter;
var emitter = new EventEmitter();
var emitter2 = new EventEmitter();
var emitter3 = new EventEmitter();
var nop = function() {}
var listener = function() {1 == 1;}