Skip to content

Instantly share code, notes, and snippets.

View cdax's full-sized avatar

Chitharanjan Das cdax

View GitHub Profile
@cdax
cdax / 1.json
Last active February 21, 2018 09:22
CCQ Demo
1
@cdax
cdax / index.html
Last active February 21, 2018 06:11
Controlled-Concurrency Queue example
<script src="https://github.com/grofers/ccq/releases/download/v0.1.0/ccq-0.1.0.min.js"></script>
<script>
new Queue()
.add(function(callback) { callback(null, 1); })
.add(function(callback) { callback(null, 2); })
.add(function(callback) { callback(null, 3); })
.await(function(results) { console.log(results); });
</script>
@cdax
cdax / gist:80f636ff2a37a0e47c73
Created February 27, 2015 07:01
If Arundhati Roy Wrote JavaScript
// 1) Start with the answer. 2) Move on to the Grubby Details.
// A viable try-able plan.
function sayIt(word) {
return TheSayItSaveItThing(word);
// Does Whatever-it-is-you-need-it-to.
// Loyal, Dependable, Weak-kneed.
function TheSayItSaveItThing(word) {
// When invoked it Saves.
@cdax
cdax / download_best.js
Created November 26, 2014 08:38
A simple file download function (supports promises via deferreds)
function _http_get(url) {
var deferred = Q.defer();
http.get(url, deferred.resolve);
return deferred.promise;
}
function download(url, filepath, progressCallback, callback) {
//http.get() promisified using Q.nfcall()
return
_http_get(url)
@cdax
cdax / download_with_nfcall.js
Last active August 29, 2015 14:10
A simple file download function (supports promises via nfcall())
function download(url, filepath, progressCallback, callback) {
//http.get() promisified using Q.nfcall()
return
Q.nfcall(http.get, url)
.then(function(res) {
//Detecting a redirect the way request/request.js does it
if(res.statusCode >= 300 && res.statusCode < 400 && hasHeader(res, 'location')) {
//Get the new URL and make a fresh call to download, returning the promise
return download(getHeader(res, 'location'), filepath, progressCallback, callback);
} else {
@cdax
cdax / download_with_deferred.js
Last active August 29, 2015 14:10
A simple file downlaod function (supports promises via deferred)
function download(url, filepath, progressCallback, callback) {
//The deferred object
var deferred = Q.defer();
http.get(url, function(res) {
//Detecting a redirect the way request/request.js does it
if(res.statusCode >= 300 && res.statusCode < 400 && hasHeader(res, 'location')) {
//Get the new URL and make a fresh call to download, returning the promise
return download(getHeader(res, 'location'), filepath, progressCallback, callback);
} else {
var data = '', totalLength = parseInt(res.getHeader('content-length'));
@cdax
cdax / download_callback_only.js
Created November 26, 2014 06:08
A simple file download function in Node.js (supports callbacks only)
function downlaod(url, filepath, progressCallback, callback) {
http.get(url, function(res) {
//Detecting a redirect the way request/request.js does it
if(res.statusCode >= 300 && res.statusCode < 400 && hasHeader(res, 'location')) {
//Get the new URL and make a fresh call to download
download(getHeader(res, 'location'), filepath, progressCallback, callback);
} else {
var data = '', totalLength = parseInt(res.getHeader('content-length'));
var file = fs.createWriteStream(filepath);
res.pipe(file);
@cdax
cdax / async_dual.js
Created November 24, 2014 11:00
Asynchronous function offering both a callback- as well as a promise-style API
var http = require('http');
var Q = require('q');
var download = function(url, callback) {
var page = '';
var deferred = Q.defer();
http.get(url, function(res) {
@cdax
cdax / async_promise.js
Last active August 29, 2015 14:10
A promise-style asynchronous function in Node
var http = require('http');
var Q = require('q');
var download = function(url) {
var page = '';
var deferred = Q.defer();
http.get(url, function(res) {
@cdax
cdax / async_callback.js
Last active August 29, 2015 14:10
A callback-style asynchronous function in Node
var http = require('http');
var download = function(url, callback) {
var page = '';
http.get(url, function(res) {
res.on('data', function(chunk) {
page += chunk.toString();