Questions tagged «serial-processing»

27
解决承诺一个接一个(即按顺序)?
考虑以下以串行/顺序方式读取文件数组的代码。readFiles返回一个promise,仅当依次读取所有文件后,promise才会解析。 var readFile = function(file) { ... // Returns a promise. }; var readFiles = function(files) { return new Promise((resolve, reject) => var readSequential = function(index) { if (index >= files.length) { resolve(); } else { readFile(files[index]).then(function() { readSequential(index + 1); }).catch(reject); } }; readSequential(0); // Start! }); }; 上面的代码可以工作,但是我不喜欢必须递归使事情顺序发生。有没有更简单的方法可以重写此代码,这样我就不必使用怪异的代码了readSequential函数了? …
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.