Skip to content

Instantly share code, notes, and snippets.

@nate-getch
nate-getch / q_example.js
Created August 28, 2017 03:50 — forked from jeffcogswell/q_example.js
Here's another chaining example on using q.js. This doesn't have any error handling, as I just want to demonstrate the chaining concept. Please read the comments carefully, as I start out with a non-q example, to show the order of flow. Please post comments if there's anything that isn't clear and I'll try to revise it as needed.
// Q sample by Jeff Cogswell
/*===========
We want to call these three functions in sequence, one after the other:
First we want to call one, which initiates an ajax call. Once that
ajax call is complete, we want to call two. Once two's ajax call is
complete, we want to call three.
BUT, we don't want to just call our three functions in sequence, as this quick
@nate-getch
nate-getch / object_level.js
Last active September 5, 2017 18:48
js code accessing var in objects of different level
var a = "one";
var obj = {
a:"two",
b: {
a:"three",
fn: function(){
return this.a;
}
}
}
@nate-getch
nate-getch / spread-rest.js
Created September 5, 2017 22:15
Spread and Rest Operator in JS ECMA6
'use strict';
var arr = ['nat', 'gat', 'bat'];
var arr2 = ['dd','bb', ...arr]; // spread
var [a, ...b] = arr; // rest
console.log(arr2); // ["dd", "bb", "nat", "gat", "bat"]
console.log(a); // nat
console.log(b); // ["gat", "bat"]
@nate-getch
nate-getch / async-node.js
Created September 6, 2017 21:18
Simple code showing Async and Sync features in Node.js
function syncfunc(a, callbackfn){
callbackfn("this is sync");
}
syncfunc('temp', function(r){ console.log(r) });
console.log("done sync func");
function asyncfunc(a, callbackfn){
process.nextTick( () => callbackfn("this is async") );
}
asyncfunc('temp', function(r){ console.log(r) });
@nate-getch
nate-getch / filter-array-proto.js
Created September 6, 2017 21:36
Extending Array object in js to filter odd and even elements of given array
const arr = [1,2,3,4,5,6,7,8];
Array.prototype.even = function(){
let r = [];
r = this.filter( v => v % 2 == 0 );
return r;
}
Array.prototype.odd = function(){
let r = [];
@nate-getch
nate-getch / node-eventloop-order.js
Created September 6, 2017 22:00
Order of execution in Event Loop of Node.js
function fun1(){
console.log('fun1 without cllback');
}
function fun2(callback){
console.log(callback('fun2 with callback'));
}
function fun3(callback){
process.nextTick(() => console.log(callback('fun3 with callback + next tick')));
}
console.log('1 ');
@nate-getch
nate-getch / node-ex-req.js
Created September 8, 2017 21:06
Node.js script to download Google logo thru external request
var http = require('http');
http.createServer(function(req, res) {
var externalReq = http.request({
hostname: "www.google.com",
path: "/images/srpr/logo11w.png"
}, function(externalRes) {
res.setHeader("content-disposition", "attachment; filename=logo.png");
externalRes.pipe(res);
});
@nate-getch
nate-getch / express.js
Created September 14, 2017 18:16 — forked from azat-co/express.js
Tutorial: REST API with Node.js and MongoDB using Mongoskin and Express.js
var express = require('express')
, mongoskin = require('mongoskin')
var app = express()
app.use(express.bodyParser())
var db = mongoskin.db('localhost:27017/test', {safe:true});
app.param('collectionName', function(req, res, next, collectionName){
req.collection = db.collection(collectionName)
@nate-getch
nate-getch / app.js
Created September 15, 2017 07:00
Node Express app example with basic functionalities
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var session = require('express-session');
var validator = require('express-validator');
var csrf = require('csurf');
@nate-getch
nate-getch / README.md
Created March 5, 2018 15:06 — forked from hofmannsven/README.md
My simply Git Cheatsheet