This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// "Write a program that prints the numbers from 1 to 100. | |
// But for multiples of three print “Fizz” instead of the number | |
// and for the multiples of five print “Buzz”. For numbers which are | |
// multiples of both three and five print “FizzBuzz”." | |
var i; | |
for (i = 1; (i <= 100); i++) { | |
var three = !(i % 3); | |
var five = !(i % 5); | |
if (three && five) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// JavaScript solver for http://blog.plover.com/math/17-puzzle.html | |
// (Cheesy yet flexible exhaustive search with recursion and eval) | |
var numbers = [ 6, 6, 5, 2 ]; | |
var operators = [ '*', '/', '+', '-' ]; | |
var goal = 17; | |
combine(numbers, function(numberSeries) { | |
if (count(operators, numbers.length - 1, function(operatorSeries) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var async = require('async'); | |
function doItAll(callback) { | |
return async.parallel([ | |
fnOne, | |
fnTwo, | |
fnThree | |
], callback); | |
function fnOne(callback) { | |
// make first request etc., invoke callback |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// in app.js | |
var _ = require('lodash'); | |
var Promise = require('bluebird'); | |
// A global context object for accessing the other modules | |
var modules = {}; | |
// Would be loaded from a server-specific file, each module has some options in its own property sometimes | |
var options = {}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
upstream upstream-mysite { | |
server localhost:3000;server localhost:3001; | |
} | |
server { | |
listen *:80; | |
server_name mysite.com; | |
client_max_body_size 32M; | |
access_log /var/log/nginx/mysite.access.log; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// test1.js | |
var from = './test2.js'; | |
try { | |
require(from); | |
} catch (e) { | |
console.log(e); | |
} | |
// test2.js |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getDb() { | |
knox = // ... init stuff, then ... | |
return knox.schema.createTableIfNotExists('users', function (table) { | |
table.bigIncrements(); | |
table.string('username', 15); | |
table.timestamps(); | |
}) | |
.then(function() { | |
knox.schema.createTableIfNotExists('posts', function(table) { | |
table.bigIncrements(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let firstStep = null; | |
if (options.count !== false) { | |
// Create a dummy promise | |
firstStep = Promise.resolve(true); | |
} else { | |
// Do actual work | |
firstStep = q.clone().count(name + '.id')... // etc. | |
} | |
return firstStep.then(... more promises go down ...) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Demonstration that promises do not involve the pyramid of doom. | |
// Native ES6 promises - you could do it with bluebird promises too, | |
// in which case Promise.delay is a built-in convenience method | |
function delays() { | |
return delay(100).then(function() { | |
return delay(100); | |
}).then(function() { | |
return delay(100); | |
}).then(function() { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ | |
{ | |
name: 'title', | |
label: 'Title' | |
}, | |
{ | |
name: 'updatedAt', | |
label: 'Last Updated', | |
partial: function(value) { | |
if (!value) { |