This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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++) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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++) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://stackoverflow.com/a/44330807/713573 | |
(async () => { | |
// sync function | |
function syncFunc (opts) { | |
return 'done'; | |
} | |
const asyncSyncFunc = async (params) => { | |
return syncFunc(params); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); |
NewerOlder