Skip to content

Instantly share code, notes, and snippets.

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 / promise-router.js
Created December 25, 2014 22:31
Using Express.js with Promises
/* Without using express-promise-router...
* If either of the two Promise-returning methods ends up failing (ie. rejecting),
* an uncaught exception will be printed to stdout, and the client will never get
* a response - instead, they'll be stuck on an infinitely loading page.
*/
express = require("express").router();
router.get("/", function(req, res) {
Promise.try(function(){
@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({
@joepie91
joepie91 / gulp.bundleLogger.js
Last active May 26, 2023 19:38
gulpfile for CoffeeScript + LiveReload + some other stuff
/* bundleLogger
------------
Provides gulp style logs to the bundle method in browserify.js
*/
var gutil = require('gulp-util');
var prettyHrtime = require('pretty-hrtime');
var startTime;
module.exports = {
"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",