Promise chain split执行多个异步任务

时间:2015-09-24 14:35:08

标签: javascript node.js asynchronous es6-promise

我有一个promise链,最后我要执行并行多个瀑布操作。

fetchFiles(conf.src, conf.dest)
            .then(function(result) {
                return filterFiles(result, conf);
            })                                             // Here
            .catch(function(error) {
                console.log("Fail: " + error);
            });

here位置,我有一系列文件。我想foreach结果>打开文件>变换>保存文件

对操作不感兴趣,只是如何将操作连接在一起

我正在尝试在ES6中这样做

1 个答案:

答案 0 :(得分:1)

我认为您正在寻找以下内容。为方便起见,我仅将Bluebird用于.delay方法,以模拟不同时间长度的不同任务。其余应该在ES2015承诺上运行。您应该能够复制粘贴整个事物npm install bluebird,然后运行代码。您将看到所有各种任务并行运行并异步完成并且无序。当然,对于每个文件,任务opentransformsave按顺序发生。 (我希望没有人反对我新获得的分号厌恶)

'use strict'

var Promise = require('bluebird') // not needed for the actual solution

let inputFiles=[1,2,3,4,5,6,7,8,9,10,11,12]

Promise.resolve(inputFiles)  // this is your array of inputfiles
 .then(  files => {
    return files.map( file => {   // map each file to a promise sequence
      return openFile(file)
              .then(transformFile)
              .then(saveFile)
              .then(allDone)
    })
  })
 .catch( error => {
   console.error("Fail: " + error);
 })

// need to generate a promise for the opened file
function openFile(file) {
 return Promise.delay( file + '_opened',  Math.floor( Math.random() * 1000));
}

// these tasks don't need to return a promise, I'm just using
// Promise.delay as a convenience to simulate asynchrony
function transformFile(file) {
 console.log(`File ${file} opened ... Transforming`)
 return Promise.delay( file + '_transformed',  Math.floor( Math.random() * 1000));
}

function saveFile(file) {
 console.log(`File ${file} transformed ... Saving`)
 return Promise.delay( file + '_saved', Math.floor( Math.random() * 1000));
}

function allDone(file) {
 console.log(`File ${file} completed ... `)
}

以上将生成如下输出:

File 6_opened opened ... Transforming
File 4_opened opened ... Transforming
File 7_opened opened ... Transforming
File 11_opened opened ... Transforming
File 7_opened_transformed transformed ... Saving
File 6_opened_transformed transformed ... Saving
File 3_opened opened ... Transforming
File 2_opened opened ... Transforming
File 11_opened_transformed transformed ... Saving
File 10_opened opened ... Transforming
File 4_opened_transformed transformed ... Saving
File 8_opened opened ... Transforming
File 12_opened opened ... Transforming
File 10_opened_transformed transformed ... Saving
File 5_opened opened ... Transforming
File 1_opened opened ... Transforming
File 3_opened_transformed transformed ... Saving
File 9_opened opened ... Transforming
File 2_opened_transformed transformed ... Saving
File 9_opened_transformed transformed ... Saving
File 4_opened_transformed_saved completed ...
File 11_opened_transformed_saved completed ...
File 6_opened_transformed_saved completed ...
File 7_opened_transformed_saved completed ...
File 8_opened_transformed transformed ... Saving
File 1_opened_transformed transformed ... Saving
File 12_opened_transformed transformed ... Saving
File 8_opened_transformed_saved completed ...
File 5_opened_transformed transformed ... Saving
File 9_opened_transformed_saved completed ...
File 10_opened_transformed_saved completed ...
File 5_opened_transformed_saved completed ...
File 3_opened_transformed_saved completed ...
File 2_opened_transformed_saved completed ...
File 12_opened_transformed_saved completed ...
File 1_opened_transformed_saved completed ...