gpt4 book ai didi

javascript - Promise 链拆分以执行多个异步任务

转载 作者:太空宇宙 更新时间:2023-11-04 00:50:28 25 4
gpt4 key购买 nike

我有一个 promise 链,我想在其末尾执行并行的多个 waterfall 操作。

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

此处位置我有一个文件数组。我想foreach结果>打开文件>转换>保存文件

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

我正在尝试在 ES6 中执行此操作

最佳答案

我认为以下内容就是您要寻找的内容。为了方便起见,我仅将 Bluebird 用于 .delay 方法,以模拟需要不同时间长度的不同任务。其余的应该在 ES2015 promise 上运行。您应该能够复制粘贴整个内容,npm install bluebird,然后运行代码。您将看到所有不同的任务都是并行运行的,并且异步且无序地完成。当然,对于每个文件,任务打开转换保存按顺序发生。 (我希望没有人反对我最近对分号的厌恶)

'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 ...

关于javascript - Promise 链拆分以执行多个异步任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32764123/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com