如何使.then方法成为async.waterfall节点js

时间:2019-01-06 05:38:47

标签: node.js

我想将.then promise方法更改为async.waterfall

var readfile = require('./app.js')
var fileName = 'batch1.txt'
var fileName1 = 'batch2.txt'

readfile(fileName).then((message) => {
    readfile(fileName1).then((message1) => {
        console.log(message);
        console.log(message1);
    });
});

2 个答案:

答案 0 :(得分:0)

输出未定义。

 var readfile = require('./helper.js')

var readFile = async function (filename1, filename2) {
    var data1 = await readfile(filename1)
    var data2 = await readfile(filename2)
    return { data1: data1, data2: data2 }
}

var data = readFile('batch1.txt', 'batch2.txt')
console.log(data.data1)
console.log(data.data2)

答案 1 :(得分:0)

尝试这个:

var readfile = require('./app.js')
var fileName = 'batch1.txt'
var fileName1 = 'batch2.txt'

readfile(fileName)
.then(() => readfile(fileName1) )
.then( (msg)=> console.log(msg));

或者您可以使用Promise.all

Promise.all([ readfile(fileName), readfile(fileName1) ]).then( ( results // Array ) =>{
    console.log(results[0]); // result of first promise
    console.log(results[1]); // result of second promise
});