node.js - 如何将 .then 方法制作为 async.waterfall Node js

标签 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);
    });
});

最佳答案

试试这个:

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
}); 

关于node.js - 如何将 .then 方法制作为 async.waterfall Node js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54058943/

相关文章:

javascript - 带有 node.js promise 的 RPC

android - 无法在 Android 手机上运行 node.js?

node.js - 如何使用 pg-promise 助手返回插入查询结果值

ios - 将 Socket IO 与 Express JS 结合使用

node.js - Hapi.js 和 Socket.io——socket.io.js 在哪里?

node.js - AMQP NodeJS 连接

node.js - Moment.js 将日期设置为落后 1 天

node.js - Electron app.on ('ready' ...永远不会被调用并且永远不会显示 Electron 窗口。似乎与特定的 Git 存储库相关联

node.js - 从 hapi 路由中的异步函数返回

javascript - 在 Firebase 中实现 session cookie 后无法读取属性 'getIdToken'