为什么这个 node.js 代码没有串行执行?

why does this node.js code not execute in series?

我是一个完全的节点菜鸟,几乎不知道自己在做什么。我正在尝试使用 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正在研究回调函数,所以你需要匿名传递它才能使期货执行下一个函数:

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在不断变化。为什么不使用更强大和流行的模块 async。它拥有您可能需要进行此类操作的一切。

你要的是async.serieshttps://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']
});