gpt4 book ai didi

Node.js 批量下载和 Bluebird promise

转载 作者:太空宇宙 更新时间:2023-11-03 23:05:07 25 4
gpt4 key购买 nike

我正在为 Node.js 编写一个批量下载器,并试图理解 bluebird 的 promise 。我想限制并行请求和磁盘写入的数量。据我了解, Promise.map(){concurrent: } 应该做我想要的。

因为 pipe()http.get() 无法自动被 promise ,所以我尝试使用自定义 promise 。

但是我不完全理解then()机制。对我来说,听起来返回的 promise 只有在整个链条都得到履行时才应该履行。

但是,在我的代码中,map() 似乎只等待链中的第一个 Promise,并且许多请求和磁盘写入是并行发生的。

import Promise from 'bluebird';
import fs from 'fs';
import https from 'https';

Promise.promisifyAll(fs);
Promise.map(Images, image => {
console.log("Opening image " + image.id);
let file = fs.createWriteStream(dir + '/' + image.id + '.jpg');
return new Promise((resolve, reject) => {
console.log("Downloading image " + image.id);
https.get(image.url, resolve).on("error", reject);
})
.then(response => {
response.pipe(file);
console.log("Saving image " + image.id);
return new Promise((resolve, reject) => {
file.on("finish", resolve);
file.on("error", reject);
});
})
.then(() => {
console.log("Finished writing image " + image.id);
file.close();
})
.catch(e => {
console.log("Error during image save of " + image.id + ": " + e.code)
});
}, {concurrent: 50})
.then(res => {
console.log("Finished writing all images")
})
.catch(e => {
console.log("Some images failed to be written: " + e.code)
});
}

我做错了什么?你能帮我理解 promise 履行和拒绝的流程吗?

最佳答案

据我所知,您正在尝试有希望地下载多个图像。实际上你不需要 promise fs。您应该使用 request 模块以便更轻松地下载。

这是我能想到的最短的工作示例

var Promise = require('bluebird');
var path = require('path');
var fs = require('fs');
var request = require('request');

var images = [{
url: 'http://bluebirdjs.com/img/logo.png',
file_name: 'bluebird.png'
}, {
url: 'http://design.ubuntu.com/wp-content/uploads/ubuntu-logo32.png',
file_name: 'ubuntu.png'
}, {
url: 'https://www.raspberrypi.org/wp-content/uploads/2012/03/raspberry-pi-logo.png',
file_name: 'raspberry-pi.png'
}];

// To Download Serially
Promise.each(images, image => new Promise((resolve, reject) => {
console.log('Downloading Image: ' + image.file_name);
request(image.url).on('error', reject).pipe(fs.createWriteStream(path.join(__dirname, image.file_name))).on('finish', () => {
console.log('Downloaded Image: ' + image.file_name);
resolve();
});
})).then(() => {
console.log('All Image Downloaded!');
}).catch(err => {
console.error('Failed: ' + err.message);
});

// To Download in Parallel (with 2 maximum concurrent jobs)
Promise.map(images, image => new Promise((resolve, reject) => {
console.log('Downloading Image: ' + image.file_name);
request(image.url).on('error', reject).pipe(fs.createWriteStream(path.join(__dirname, image.file_name))).on('finish', () => {
console.log('Downloaded Image: ' + image.file_name);
resolve();
});
}), {
concurrency: 2
}).then(() => {
console.log('All Image Downloaded!');
}).catch(err => {
console.error('Failed: ' + err.message);
});

关于Node.js 批量下载和 Bluebird promise ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36663928/

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