Skip to content

Instantly share code, notes, and snippets.

View Eli-Goldberg's full-sized avatar

Eli Golderg Eli-Goldberg

View GitHub Profile
@Eli-Goldberg
Eli-Goldberg / elasticsearch-nginx-proxy-ec2
Last active November 30, 2017 16:28
Nginx Proxy - Elastic Search
# Be sure to replace the three <> parts
# New ubuntu machines annoying locale warnings
#sudo locale-gen en_US en_US.UTF-8
#sudo dpkg-reconfigure locales
sudo apt update -y && sudo apt upgrade -y && sudo apt autoremove -y
sudo apt-get install nginx -y
sudo systemctl status nginx
sudo systemctl start nginx
@Eli-Goldberg
Eli-Goldberg / async-waterfall.js
Last active March 29, 2017 13:58
Using async-waterfall to avoid callback hell
const waterfall = require('async-waterfall');
waterfall([
function(callback){
callback(null, 'one', 'two');
},
function(arg1, arg2, callback){
callback(null, 'three');
},
function(arg1, callback){
@Eli-Goldberg
Eli-Goldberg / callback-hell.js
Created March 29, 2017 13:53
Callback hell
function one() {
setTimeout(function() {
console.log('1. First thing setting up second thing');
setTimeout(function() {
console.log('2. Second thing setting up third thing');
setTimeout(function() {
console.log('3. Third thing setting up fourth thing');
setTimeout(function() {
console.log('4. Fourth thing');
}, 2000);
@Eli-Goldberg
Eli-Goldberg / node-callback.js
Created March 29, 2017 13:49
Node style callbacks
fs.readFile('/foo.txt', function(err, data) {
// TODO: Error Handling Still Needed!
console.log(data);
});
@Eli-Goldberg
Eli-Goldberg / thenables.js
Created March 29, 2017 13:36
Chaining thenables
const q = require('q');
function doSomething(data) {
data.one = 1;
return q(data);
}
function somethingElse(data) {
const deferred = q.defer();
setTimeout(function() {
@Eli-Goldberg
Eli-Goldberg / typof-async-function.js
Created March 29, 2017 13:27
typeof async function
typeof async function(){}
/// 'function'
@Eli-Goldberg
Eli-Goldberg / async-loop.js
Created March 29, 2017 13:14
Using async-await to iterate Promises
async function waitAndPrint(toPrint) {
await new Promise((resolve, reject) => {
setTimeout(() => {
console.log(toPrint);
resolve();
}, Math.random() * 1000);
});
}
const arrayOfNumbers = [1, 2, 3, 4];
@Eli-Goldberg
Eli-Goldberg / coPromise.js
Created March 29, 2017 13:02
Co with Promises
const co = require('co');
const request = require('request');
const bluebird = require('bluebird');
const requestPromisified = bluebird.promisifyAll(request); // This just adds Async to everything
function* goToThere(url) {
console.log('Just started');
const someData = yield const requestPromisified.getAsync(url)
console.log('Yes, I DO have data ' + someData);
return someData;
@Eli-Goldberg
Eli-Goldberg / foreach-await.js
Last active March 29, 2017 11:48
forEach await
async function waitAndPrint(toPrint) {
await new Promise((resolve, reject) => {
setTimeout(() => {
console.log(toPrint);
resolve();
}, Math.random() * 1000);
});
}
const arrayOfNumbers = [1, 2, 3, 4];