Pregunta

I have this readLines function to parse line by line that called from:

var fs = require('fs');
var Q = require('q');

Q.all(readLines(fs.createReadStream("/tmp/test.txt"), console.log)).then(function () {
    console.log('Done');
};

function readLines(input, func) {
    var remaining = '';

    input.on('data', function (data) {
        remaining += data;
        var index = remaining.indexOf('\n');
        while (index > -1) {
            var line = remaining.substring(0, index);
            remaining = remaining.substring(index + 1);
            func(line);
            index = remaining.indexOf('\n');
        }
    });

    input.on('end', function () {
        if (remaining.length > 0) {
            func(remaining);
        }
    });
};

Could anyone help why I never got "Done"? Any tutorial to understand how Promises work?

¿Fue útil?

Solución

This will work better for you. Please read the comments in the code - it's actually only a few extra lines, and that was including adding error handing.

var fs = require('fs');
var Q = require('q');

// you don't need Q.all unless you are looking for it to resolve multiple promises.
// Since readlines returns a promise (a thenable), you can just then() it.
readLines(fs.createReadStream("./vow.js"), console.log).then(function (x) {
    console.log('Done',  x);
});

function readLines(input, func) {
    // you need to create your own deferred in this case - use Q.defer()
    var deferred = Q.defer();
    var remaining = '';

    input.on('data', function(data) {
        remaining += data;
        var index = remaining.indexOf('\n');
        while (index > -1) {
            var line = remaining.substring(0, index);
            remaining = remaining.substring(index + 1);
            func(line); 
            index = remaining.indexOf('\n');
        }
    });

    input.on('end', function() {
        if (remaining.length > 0) {
            func(remaining);
            console.log('done');
        }
        // since you're done, you would resolve the promise, passing back the 
        // thing that would be passed to the next part of the chain, e.g. .then()
        deferred.resolve("Wouldn't you want to return something to 'then' with?");
    });

    input.on('error', function() {
        console.log('bother');
        // if you have an error, you reject - passing an error that could be
        // be caught by .catch() or .fail()
        deferred.reject(new Error('Regrettable news - an error occured.'));
    });
    // you return the promise from the deferred - not the deferred itself
    return deferred.promise;
};
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top