为什么这个node.js代码不能串行执行?

我是一个总节点noob,几乎不知道我在做什么。 我试图依次执行一系列function,使用futures库。 我的代码:

 var futures = require('futures'); var sequence = futures.sequence(); sequence .then(function() { console.log("one"); }) .then(function() { console.log("two"); }) .then(function() { console.log("three"); }); 

我希望我的输出是

 one two three 

但我得到的输出是

 one 

我究竟做错了什么?

Node.js正在处理callback函数,所以你需要通过匿名方式来传递它,使期货执行下一个函数:

 var futures = require('futures'); var sequence = futures.sequence(); sequence .then(function(next) { console.log("one"); next(null, 1); }) .then(function(next) { console.log("two"); next(null, 2); }) .then(function(next) { console.log("three"); next(null, 3); }); 

futures正在不断变化和变化。 为什么不使用更强大和stream行的模块async 。 它拥有您可能需要的所有这些操作。

你在做什么是async.series https://github.com/caolan/async#seriestasks-callback

 async.series([ function(callback){ // do some stuff ... callback(null, 'one'); }, function(callback){ // do some more stuff ... callback(null, 'two'); } ], // optional callback function(err, results){ // results is now equal to ['one', 'two'] });