第一次调用后Promise函数无法解析

时间:2018-10-05 01:01:30

标签: node.js promise

我有一个对文本文件进行排序的功能。我有几个文件需要一次排序。我将每个电话链接在一起。首次调用sort函数可以正常工作。文件排序后,将关闭流并调用resolve。其他文件调用排序功能,但它们始终无法解析。我不知道问题出在排序功能中还是在打电话。

排序功能是...

var spawn = require('child_process').spawn;
var fs = require('fs');

var sortFile = function(dir, file, type) {
    var sortProcess = spawn('sort', [file]);
    sortProcess.stdout.setEncoding('utf8');

    return new Promise(function(resolve, reject) {
        var fileStream = fs.createWriteStream(dir + '/' + type + '_sorted.txt', { flags: 'a' });

        sortProcess.stdout.pipe(fileStream);

        sortProcess.stderr.on('data', function (data) {
            reject(err);
        });
        sortProcess.on('close', function (code) {
            resolve();
        });
    });
}

module.exports.sortFile = sortFile;

我这样称呼。

fileSort.sortFile(dir, students_File, 'student')
                .then(function() {
                    students_stream = fs.createReadStream(dir + '/student_sorted.txt');
                    fileSort.sortFile(dir, schools_File, 'school')
                })
                .then(function() {
                    schools_Stream = fs.createReadStream(dir + '/school_sorted.txt');
                    fileSort.sortFile(dir, staff_File, 'staff');
                })
                .then(function() {
                    staff_Stream = fs.createReadStream(dir + '/staff_sorted.txt');
                    fileSort.sortFile(dir, classes_File, 'class');
                })
                .then(function() {
                    classes_Stream = fs.createReadStream(dir + '/class_sorted.txt');
                    fileSort.sortFile(dir, courseCatalog_File, 'course');
                })
                .then(function() {
                    courseCatalog_Stream = fs.createReadStream(dir + '/course_sorted.txt');
                    fileSort.sortFile(dir, secondaryTeachers_File, 'secTeachers');
                })
                .then(function() {
                    secondaryTeachers_Stream = fs.createReadStream(dir + '/secTeachers_sorted.txt');

0 个答案:

没有答案