Skip to content

Instantly share code, notes, and snippets.

@joepie91
joepie91 / promises.coffee
Created August 29, 2014 18:15
Promises in CoffeeScript
someAsyncMethod()
.then (result) ->
DoSomethingSynchronous()
return someOtherAsyncMethod("stuff")
.then (result) ->
return yetAnotherAsyncMethod("things", "and stuff")
.then (result) ->
console.log "Whoo, we're done!"
exit 0
someAsyncMethod().then(function(result) {
DoSomethingSynchronous();
return someOtherAsyncMethod("stuff");
}).then(function(result) {
return yetAnotherAsyncMethod("things", "and stuff");
}).then(function(result) {
console.log("Whoo, we're done!");
return exit(0);
});
var shelf = /* initialize bookshelf here */
app.use(function(req, res, next) {
req.db = shelf;
req.model = shelf.model.bind(shelf);
return next();
});
@joepie91
joepie91 / promises.js
Last active August 29, 2015 14:08
Promises
/* Without Promises */
asynchronousFunctionOne(function(value){
somethingSync(value);
asynchronousFunctionTwo(function(){
somethingElseSync();
asynchronousFunctionThree(function(){
console.log("Done!");
});
});
});
// BAD:
if (condition == true)
doThing();
// GOOD:
if (condition == true) {
doThing();
}
@joepie91
joepie91 / README.md
Created December 24, 2014 05:51
fancyshelf documentation (pre-release)

fancyshelf

A friendly wrapper for Bookshelf. The goal is to have an API that is so consistent, predictable and logical that you won't even really need to read the documentation to use it.

The registry and virtuals plugins in Bookshelf are enabled by default when using fancyshelf.

Instantiating

var shelf = require("fancyshelf")({
Promise = require "bluebird"
appUtil = require "../util"
module.exports = (shelf) ->
shelf.model "User",
tableName: "users"
hasTimestamps: true
projects: -> @hasMany "Project", "owner_id"
@joepie91
joepie91 / stuff.js
Created January 3, 2015 18:00 — forked from mderijcke/stuff.js
function http_get(url, { "User-Agent" = "kitchen sink 2.0" }, { sayHi, verbose: v }) {
if (sayHi) {
console.log("HI!");
}
if (v) {
console.log("Sending request");
}
return require('http').get({
"devDependencies": {
"browserify": "^5.11.2",
"browserify-shim": "^3.7.0",
"coffee-script": "^1.8.0",
"coffeeify": "^0.7.0",
"gulp": "~3.8.0",
"gulp-cached": "~0.0.3",
"gulp-coffee": "~2.0.1",
"gulp-concat": "~2.2.0",
"gulp-jade": "^0.7.0",
@joepie91
joepie91 / middleware.js
Created January 12, 2015 23:41
Using a persistent object in Express
someObject = instantiateSomePersistentObjectSomehow();
app.use(function(req, res, next) {
req.someObject = someObject;
next();
});
// ...
app.get("/sample/route", function(req, res) {