nodejs中的jQuery.when()相当于什么?

我问jquery是什么时候在angular度 ,现在我想在节点做类似的事情。 我需要这样的东西:

when(fs.readFile('file1'), fs.readFile('file2')) .done(function( a1, a2 ) { ... // do stuff });

我怎样才能做到这一点? 谢谢。

你需要节点0.11.13或更高的工作或使用一个库,但这将是Promise.all

 var Promise = require("bluebird"); // Imports the Bluebird promise library in // new versions of node this is optional Promise.promisifyAll(fs); // this is required if using Bluebird, you'll have to // do this manually with native promises. Promise.all([fs.readFileAsync('file1'), fs.readFileAsync('file2')]) .spread(function( a1, a2 ) { ... // do stuff }); 

至于如何将callbackAPI转换为承诺 – 请参阅此问题和答案 。