Skip to content

Instantly share code, notes, and snippets.

@EGreg
EGreg / pipe.js
Created December 16, 2010 01:22
Lets you pipe data and create control flow structures
/*
* Creates a callback to be called after multiple functions are done
* Usage:
* var p = Q.pipe(function (params, subjects) {
* // arguments that were passed are in params.user, params.stream
* // this objects that were passed are in subjects.user, subjects.stream
* }, ['user', 'stream]);
* mysql("SELECT * FROM user WHERE user_id = 2", p.fill('user'));
* mysql("SELECT * FROM stream WHERE publisher_id = 2", p.fill('stream'));
*
@EGreg
EGreg / pipe.js
Created December 16, 2010 02:40
Piping baby
/*
* Sets up control flows involving multiple callbacks and dependencies
* Usage:
* var p = Q.pipe(function (params, subjects) {
* // arguments that were passed are in params.user, params.stream
* // this objects that were passed are in subjects.user, subjects.stream
* }, ['user', 'stream]);
* mysql("SELECT * FROM user WHERE user_id = 2", p.fill('user'));
* mysql("SELECT * FROM stream WHERE publisher_id = 2", p.fill('stream'));
*
@EGreg
EGreg / openletter.txt
Created May 3, 2011 00:33
The most open letter ever
This is the most open letter ever. Other authors claim their letters are "open" because you can all read them, but in reality, they are nothing but the rants of one person, which you might not even completely agree with, or will piss you off.
By contrast, this letter is truly open and gives you the freedom to fork and modify it. Pissed about Mike Arrington investing? Want to give Richard Stallman some personal advice to stop publicly mentioning conspiracy theories he may or may not believe in? The choice is yours. Simply fork this letter and post it on Hacker News and other places. Track its changes, and see what happens.
Anything you wanted to write about lately?
--- THE THREE LINES ---
This letter is licensed under the GNU Public License. http://www.gnu.org/licenses/gpl.html
You may fork it but you must include these three lines exactly as they appear at the bottom of the letter.
@EGreg
EGreg / DRY
Created January 25, 2012 20:13 — forked from fennb/gist:1124579
Asynchronous cache failover example
// asynchronous version
asynchronousCache.get("id:3244", function(err, myThing) {
if (myThing == null) {
asynchronousDB.query("SELECT * from something WHERE id = 3244", function(err, myThing) {
// We now have a thing from DB, do something with result
doSomething(myThing);
});
} else {
// We have a thing from cache, do something with result
@EGreg
EGreg / gist:1678415
Created January 25, 2012 20:17 — forked from fennb/gist:1124580
Asynchronous loop issues illustrative example
// Illustrative case
// YEAH -- ILLUSTRATIVE OF THE FACT THAT YOU CAN ISSUE PARALLEL QUERIES TO THE DATABASE INSTEAD OF SERIAL ONES LIKE A DUMB THREADED IMPLEMENTATION THAT IS INFERIOR FOR DOING WEB SERVERS
function (recentBlogPostIds, callback) { // obviously you need this, but use callback instead of return
var results = {};
var count = 0;
for (var i = 0; i < recentBlogPostIds.length; i++) {
var blogPostId = recentBlogPostIds[i];
@EGreg
EGreg / gist:4602842
Created January 23, 2013 07:26
Creates mixins and classes in JS
/**
* Mixes in one or more classes. Useful for inheritance and multiple inheritance.
* @param A Function
* The constructor corresponding to the "class" we are mixing functionality into
* This function will get the following members set:
* __mixins: an array of [B, C, ...]
* constructors(subject, params): a method to call the constructor of all mixing classes, in order. Pass this to it.
* staticProperty(property): a method for getting a property name
* @param B Function
* One or more constructors representing "classes" to mix functionality from
@EGreg
EGreg / examples.txt
Last active December 11, 2015 22:58
Examples of how to use Q
SERVER SIDE:
========================= classes/Chess.php =====================
// MODEL ON SERVER
class Chess extends Base_Chess // here's one particular table
{
function makeMove($e)
{
live server:
> hg pull
> hg branch
default
> hg merge develop
36 files updated, 0 files merged, 0 files removed, 0 files unresolved
(branch merge, don't forget to commit)
> hg ci -m "merged develop - should fix the bugs"
created new head (!?)
[root@li226-103 Q]$ hg strip 2970
saved backup bundle to /live/Q/.hg/strip-backup/d81bd91d5db2-backup.hg
Started to generate API documentation
[root@li226-103 Q]$ hg log | head
2970[tip]:2967,2969 3c779bf0f46f 2013-03-21 10:25 -0400 gregory
merged develop - hopefully this will fix the bugs
2969 417d2a852997 2013-03-21 04:28 +0400 dmitriy
Removed 'behavior' css3pie property from css rules and made separate UI-ie.css files with that 'behavior' property.
@EGreg
EGreg / Mysql.php
Created April 23, 2013 22:58
testing
<?php
/**
* @module Db
*/
class Db_Mysql implements iDb
{
/**
* This class lets you create and use PDO database connections.