Skip to content

Instantly share code, notes, and snippets.

@domenic
domenic / di-example.java
Created April 17, 2012 00:20
StackOverflow DI example
public class A
{
private Bable b;
private Cable c;
public A(Bable b, Cable c)
{
this.b = b;
this.c = c;
}
@domenic
domenic / example.js
Last active May 25, 2018 10:17
Promise chaining example
// `promise` is some operation that may succeed (fulfill) or fail (reject)
var newPromise = promise.then(
function () {
return delay(1000);
},
writeError
);
// If `promise` fulfills, `newPromise` will fulfill in 1000 ms.
// If `promise` rejects and writing to the error log succeeds,
@domenic
domenic / auth-spec.js
Created April 20, 2012 20:54
auth spec
describe("when grant_type is password", function () {
describe("and client_id/client_secret from Authorization header are valid", function () {
describe("and username/password pass user authentication", function () {
it("should grant an access token in the appropriate response format", function (done) {
describe("and username/password do not pass user authentication", function () {
itShouldRejectWith(restify.UnauthorizedError, "invalid_client");
describe("and the user authentication process fails in an unexpected way", function () {
it("should pass that error through", function (done) {
@domenic
domenic / 1flickrApi.js
Last active April 4, 2018 07:35
Flickr API experiments
"use strict";
// Uses jQuery and Q.
var API_KEY = "whatever";
function getFlickrResponseAsync(method, responseProperty, params) {
var deferred = Q.defer();
$.ajax("http://www.flickr.com/services/rest/", {
@domenic
domenic / q-mongoose-so.js
Last active August 24, 2017 05:49
Q + Mongoose from StackOverflow
var mongoose = require('mongoose');
mongoose.connect('mongo://localhost/test');
var conn = mongoose.connection;
var users = conn.collection('users');
var channels = conn.collection('channels');
var articles = conn.collection('articles');
var insertUsers = Q.nfbind(users.insert.bind(users));
var insertChannels = Q.nfbind(channels.insert.bind(channels));
@domenic
domenic / q-readme-helpers.md
Created May 16, 2012 05:04
Potential addition to Q readme

Using Static Helpers

There are some basic patterns for creating promises that are so common, you will want to use Q's static helpers for them. The most important of these are Q.resolve and Q.reject.

Use Q.resolve to create either a fulfilled promise, if you pass a fulfillment value directly, or to simply subsume another promise. That is, in general,

@domenic
domenic / license-types.js
Created May 25, 2012 19:38
License type translation code
var LICENSE_TYPE = Object.freeze({
UNRESTRICTED: 0,
BASIC: 1,
ENHANCED: 2,
SOCIAL: 3,
FREE_TRIAL: 4
});
productTranslations.unrestrictedLicenseFormatCodes = ["EE", "EF", "EG", "EY", "E9", "D8", "DX"];
productTranslations.socialLicenseFormatCodes = ["EP", "EH", "DU", "DW"];
@domenic
domenic / portable-node.md
Created May 25, 2012 21:03
Tips for Writing Portable Node.js Code

Node.js core does its best to treat every platform equally. Even if most Node developers use OS X day to day, some use Windows, and most everyone deploys to Linux or Solaris. So it's important to keep your code portable between platforms, whether you're writing a library or an application.

Predictably, most cross-platform issues come from Windows. Things just work differently there! But if you're careful, and follow some simple best practices, your code can run just as well on Windows systems.

Paths and URLs

On Windows, paths are constructed with backslashes instead of forward slashes. So if you do your directory manipulation

@domenic
domenic / q-nexttick-times.md
Created May 27, 2012 06:30
Q test suite nextTick results

Comparing Different Implementations of process.nextTick using the Q test suite.

Used: q-spec.js@a1a416.

Implementations compared:

Based on MessageChannel

This is currently in Q:

@domenic
domenic / defineProperty.md
Created May 30, 2012 21:27
Object.defineProperty talk notes

Opening Remarks

  • Fun feature of the JS language that few know about, and even fewer use.
  • It's in "ECMAScript 5" (explain this).
  • Browser support: IE9 or above. Plus MOBILE!!

Series of Demos

Getters