nicklasos
2/5/2016 - 3:08 PM

Node.js nodejs node

Node.js nodejs node

'use strict';

class TaskQueue {
    constructor(limit) {
        this.limit = limit;
        this.running = 0;
        this.tasks = [];
    }

    add(task) {
        this.tasks.push(task);
        this.next();
    }

    next() {
        if (this.running < this.limit && this.tasks.length) {
            var task = this.tasks.shift();
            this.running++;

            task(() => {
                this.running--;
                this.next();
            });
        }
    }
}

var tasks = new TaskQueue(2);

tasks.add(function (done) {
    setTimeout(function () {
        console.log('Done first');
        done();
    }, 1000);
});

tasks.add(function (done) {
    setTimeout(function () {
        console.log('Done second');
        done();
    }, 1000);
});

tasks.add(function (done) {
    setTimeout(function () {
        console.log('Done third');
        done();
    }, 1000);
});
// knex
table.timestamp('created_at').defaultTo(knex.raw('CURRENT_TIMESTAMP'));
table.timestamp('updated_at').defaultTo(knex.raw('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'));
const current = {};

module.exports = function (name, seconds, callback) {
  if (!current.hasOwnProperty(name)) {
    current[name] = true;

    setTimeout(() => {
      delete current[name];
    }, seconds * 1000);

    callback();
  }
};
/**
 * Replace require with anything
 * For unit testing purposes
 *
 * monkey('node-fetch', () => console.log('replaced'));
 *
 * @param {string} module
 * @param replace
 */
function monkey(module, replace) {
  require(module);
  require.cache[require.resolve(module)].exports = replace;
}

module.exports = monkey;
import mkdirp = require('mkdirp');
import fs = require('fs');
import { dirname } from 'path';

export function writeFile(path, contents) {
    return new Promise((resolve, reject) => {
        mkdirp(dirname(path), function (err) {
            if (err) return reject(err);

            fs.writeFile(path, contents, (err) => {
                if (err) return reject(err);

                resolve();
            });
        });
    });
}
function asyncParallel(tasks, callback) {
    var results = [],
        done = 0;

    tasks.forEach(function (task) {
        task(function (result) {
            if (result) {
                results.push(result);
            }

            if (++done == tasks.length) {
                callback(results);
            }
        });
    });
}

var tasks = [
    function (done) {
        setTimeout(function () {
            done('result 1');
        }, 1000);
    },
    function (done) {
        setTimeout(function () {
            done('result 2');
        }, 1000);
    },
    function (done) {
        setTimeout(function () {
            done('result 3');
        }, 1000);
    },
    function (done) {
        setTimeout(function () {
            console.log('Done without result');
            done();
        }, 1000);
    }
];

var callback = function (results) {
    console.log(results);
};

asyncParallel(tasks, callback);