Skip to content

Instantly share code, notes, and snippets.

@gagan-bansal
gagan-bansal / decelerating-and-callback-with-test-condition.js
Last active July 20, 2023 11:36
1) example 2) decelerating: callback is called at exponential rate, till max maximum delay is reached. 3) decelerating: callback is called at exponential rate if test satisfies, till max maximum delay is reached.
// source from https://stackoverflow.com/a/7445863/713573
function decelerating (callback, opts= {}) {
const {initialDelay = 100, exp = 2, maxDelay = 10000, test} = opts;
const myFunction = function() {
console.log('[debug] time: %s, delay: %s', Date.now(), delay);
if (test()) {
callback();
delay *= exp;
if (delay > maxDelay) {
const {tryEach} = require('async');
(async () => {
const array = [12, 6 , 30, 55, 5];
const testArr = array.map(v => {
return async function test() {
console.log('val: ', v);
if (v > 20) return Promise.resolve(v);
else return Promise.reject(new Error('not found'));
const fs = require('fs');
const {LoremIpsum} = require("lorem-ipsum");
const lorem = new LoremIpsum();
const ws = fs.createWriteStream('somefile.txt');
// https://nodejs.org/api/stream.html#stream_event_drain
// Write the data to the supplied writable stream one million times.
// Be attentive to back-pressure.
function writeOneMillionTimes(writer, data, encoding, callback) {
let i = 1000000;
write();
@gagan-bansal
gagan-bansal / md2html.sh
Last active September 27, 2022 16:54
Convert all your markdown files to HTML files. Required installation of marked module (https://www.npmjs.com/package/marked).
marked --gfm README.md | sed 's/\.md">/.html">/g' > README.html
for i in `find ./docs/ -name '*md' `
do
out=`echo $i |sed "s/\.md/.html"/g`
echo $i '->' $out
marked --gfm $i | sed 's/\.md">/.html">/g' > $out
done;
@gagan-bansal
gagan-bansal / node-js-split-stream.js
Last active September 13, 2022 16:17
Node.js split stream in two streams.
// source code from https://stackoverflow.com/a/19561718/713573
const fs = require("fs");
const {Transform} = require('stream');
const split = require('split');
const readStream = fs.createReadStream('./numbers');
const evenWriteStream = fs.createWriteStream('./evens');
const oddWriteStream = fs.createWriteStream('./odds');
class Even extends Transform {
@gagan-bansal
gagan-bansal / node-js-stream-transform-with-delay.js
Last active September 13, 2022 15:39
Node.js stream transform example to fitler the data and having delay in transform function.
const fs = require("fs");
const {Transform} = require('stream');
const split = require('split');
const readStream = fs.createReadStream('./numbers');
const writeStream = fs.createWriteStream('./evens');
function test (chunk, cb) {
const val = parseInt(chunk.toString());
if (val % 2 === 0) cb(null, val.toString() + '\n');
@gagan-bansal
gagan-bansal / stream-transform-in-parallel-ordered-output.js
Created September 8, 2022 04:25
Process stream transform in parallel but output the data in parallel.
// example from parallel-transform
var transform = require('parallel-transform');
var stream = transform(5, function(data, callback) { // 5 is the parallism level
setTimeout(function() {
callback(null, data);
}, 1000);
});
for (var i = 0; i < 20; i++) {
@gagan-bansal
gagan-bansal / stream-transform-in-parallel-un-ordered-output.js
Created September 8, 2022 04:21
Process stream transform in parallel and output the data as soon as its processed.
// source from 'parallel-transform'
var transform = require('parallel-transform');
var stream = transform(5, {ordered: false}, function(data, callback) { // 10 is the parallism level
setTimeout(function() {
callback(null, data);
}, Math.random() * 1000);
});
for (var i = 0; i < 20; i++) {
// https://stackoverflow.com/a/44330807/713573
(async () => {
// sync function
function syncFunc (opts) {
return 'done';
}
const asyncSyncFunc = async (params) => {
return syncFunc(params);
@gagan-bansal
gagan-bansal / express-routers-routes-list.js
Last active July 30, 2022 13:46
With express mount routers route and list routes of router.
const express = require('express');
const app = express();
const router = express.Router();
router.get('/pets/cats', (req, res) => {
res.send('10 cats');
});
router.get('/pets/dogs', (req, res) => {
res.send('5 dogs');