Skip to content

Instantly share code, notes, and snippets.

View rafaeljesus's full-sized avatar

Rafael Jesus rafaeljesus

  • Berlin, Germany
View GitHub Profile
@rafaeljesus
rafaeljesus / bb.js
Created December 4, 2015 13:28 — forked from stephantabor/bb.js
Bluebird .each vs .mapSeries vs .map
var Promise = require('bluebird');
var funcs = Promise.resolve([500, 100, 400, 200].map((n) => makeWait(n)));
funcs
.each(iterator) // logs: 500, 100, 400, 200
.then(console.log) // logs: [ [Function], [Function], [Function], [Function] ]
funcs
.mapSeries(iterator) // logs: 500, 100, 400, 200
@rafaeljesus
rafaeljesus / promise_while_loop.js
Created December 2, 2015 19:45 — forked from victorquinn/promise_while_loop.js
Promise "loop" using the Bluebird library
var Promise = require('bluebird');
var promiseWhile = function(condition, action) {
var resolver = Promise.defer();
var loop = function() {
if (!condition()) return resolver.resolve();
return Promise.cast(action())
.then(loop)
.catch(resolver.reject);
@rafaeljesus
rafaeljesus / bootstrap.js
Created November 2, 2015 19:01 — forked from Unitech/bootstrap
PM2 / Keymetrics for Heroku/Rackspace/Joyent/Amazon Elasticbeanstalk/Azure...
// Make sure you added pm2 as a dependency in your package.json
// Then in your Procfile, do a simple `node bootstrap.js`
var pm2 = require('pm2');
var MACHINE_NAME = 'hk1';
var PRIVATE_KEY = 'z1ormi9vomgq66';
var PUBLIC_KEY = 'oa0m7nuhdfibi16';
var instances = process.env.WEB_CONCURRENCY || -1; // Set by Heroku or -1 to scale to max cpu core -1
@rafaeljesus
rafaeljesus / continuable-generators.js
Created October 8, 2015 23:00
Using continuation passing style with ES6 generators along with node's callback style functions as an alternative to promises for escaping callback hell.
"use strict";
const GeneratorFunction = function*(){}.constructor;
const GeneratorFunctionPrototype = GeneratorFunction.prototype;
const GeneratorPrototype = GeneratorFunctionPrototype.prototype;
const slice = Array.prototype.slice;
const concat = Array.prototype.concat;
const toString = Object.prototype.toString;
@rafaeljesus
rafaeljesus / codewars.rb
Created September 29, 2015 14:36 — forked from Micka33/codewars.rb
codewars exemple
#Subject
#Find the sum of the digits of all the numbers from 1 to N (both ends included).
#For N = 10 the sum is 1+2+3+4+5+6+7+8+9+(1+0) = 46
#For N = 11 the sum is 1+2+3+4+5+6+7+8+9+(1+0)+(1+1) = 48
#For N = 12 the sum is 1+2+3+4+5+6+7+8+9+(1+0)+(1+1) +(1+2)= 51
#Supported Ruby version is 1.9.3
#Tests
it 'should' do
Test.assert_equals(solution(10), 46)
@rafaeljesus
rafaeljesus / .eslintrc.js
Last active September 10, 2015 22:23 — forked from nkbt/.eslintrc.js
Strict ESLint config for React, ES6 (based on Airbnb Code style)
{
"env": {
"browser": true,
"node": true,
"es6": true
},
"plugins": ["react"],
"ecmaFeatures": {
@rafaeljesus
rafaeljesus / gist:ec0a3b4885856589469c
Last active September 8, 2015 00:40
Walmart Mobile node.js Setup

Overview

We run multiple server processes in two data centers. Each process listens on two ports, one for HTTP and one for HTTPS. HTTPS is terminated by Apache prior to reaching node.js. HTTP goes directly from the client to node.js (through a master load balancer). We do not use clusters. We slice our physical servers into thin virtual machines running SmartOS, each with about 3GB of memory designed for a single node.js process.

Our node.js servers are hapi.js servers using the composer functionality and plugins architecture. We have three sets of plugins loaded: mobile web front end experience (single page app), legacy API reverse proxy, and monitoring.

We also serve original node.js services off another server zone which runs closed source plugins using hapi.

Analytics

@rafaeljesus
rafaeljesus / boot.js
Last active August 29, 2015 14:24 — forked from jdx/boot.js
// This script will boot app.js with the number of workers
// specified in WORKER_COUNT.
//
// The master will respond to SIGHUP, which will trigger
// restarting all the workers and reloading the app.
var cluster = require('cluster');
var workerCount = process.env.WORKER_COUNT || 2;
// Defines what each worker needs to run
// Promise.all is good for executing many promises at once
Promise.all([
promise1,
promise2
]);
// Promise.resolve is good for wrapping synchronous code
Promise.resolve().then(function () {
if (somethingIsNotRight()) {
throw new Error("I will be rejected asynchronously!");