This file contains hidden or 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
function sequentially(array, promiseFn) { | |
return array.reduce((acc, current) => | |
acc.then(results => | |
promiseFn(current).then(res => | |
results.concat(res) | |
) | |
) | |
, Promise.resolve([]); | |
} |
This file contains hidden or 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 Transform = require('stream').Transform; | |
const util = require('util'); | |
function ChunkStream(options) { | |
if (!(this instanceof ChunkStream)) | |
return new ChunkStream(options); | |
Transform.call(this, options); | |
this.chunkSize = options.chunkSize || 1500; | |
this._chunks = [ ]; |
This file contains hidden or 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
// paginate('get', '/foo', {...}) | |
const paginate = (method, path, opts, offset, results) => { | |
offset = offset || 0; | |
results = results || [ ]; | |
return Promise.try(() => | |
request(method, path, Object.assign(opts, { offset })) | |
).then(res => { | |
let newResults = results.concat(res.data); | |
if (!res.pagination) { return newResults; } |
This file contains hidden or 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
function getFoos(nextOffset, results) { | |
nextOffset = nextOffset || 0; | |
results = results || [ ]; | |
return Promise.try(() => { | |
return getSomeFoos(nextOffset); | |
}).then(res => { | |
let newResults = results.concat(res.foos); | |
return res.hasMore ? | |
getFoos(res.nextOffset, newResults) : |
This file contains hidden or 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
var qb = knex.select('songs.id', 'songs.name AS song', 'artists.name AS artist', 'songs.path', 'albums.name AS album') | |
.from('songs') | |
.join('genreAssociation', 'songs.id', 'genreAssociation.id_songs') | |
.join('artists', 'songs.id_artists', 'artists.id') | |
.join('albums', 'songs.id_albums', 'albums.id') | |
.where('genreAssociation.id', req.params.id) | |
if (req.session.playedSongs && req.session.playedSongs.length != 0 && req.session.playedSongs != []) { | |
qb.whereNotIn('songs.id', req.session.playedSongs); | |
} |
This file contains hidden or 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
foo | bar | |
---|---|---|
1 | 2 | |
3 | 4 | |
5 | 6 | |
7 | 8 |
This file contains hidden or 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
var split = require('binary-split-streams2'), | |
fs = require('fs'); | |
function requestStream(url) { | |
return Kefir.stream(emitter => { | |
let req = bhttp.get(url, { stream: true }) | |
.pipe(split()); // default is \n -- no regex support, use a different module if you need variable line terminators | |
req.on('data', chunk => emitter.emit(chunk)); | |
req.on('error', err => { | |
emitter.error(err); emitter.end(); |
This file contains hidden or 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
var EventEmitter = require('events').EventEmitter, | |
inherits = require('util').inherits; | |
var ID = 0; | |
function Fetcher(url) { | |
EventEmitter.call(this); | |
this.id = ID++; | |
this.url = url; | |
} |
This file contains hidden or 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
myndzi@seckzi:~/test$ node foo | |
pg pool: function (options) { | |
var config = { Client: Client }; | |
for (var key in options) { | |
config[key] = options[key]; | |
} | |
Pool.call(this, config); | |
} | |
myndzi@seckzi:~/test$ npm ls | |
[email protected] /home/myndzi/test |
This file contains hidden or 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
function delay(ms) { | |
// error checking wouldn't hurt | |
return new Promise(resolve => setTimeout(resolve, delay)); | |
} | |
function retryWithDelay(fn, delay, retries) { | |
if (retries <= 0) { | |
return Promise.reject(new Error('out of retries')); | |
} | |
return fn().catch(err => { | |
// typed catch or checking the kind of error here would be a good thing |