如何在 node.js 中的特定持续时间后强制解决承诺? [英] How to force resolve a promise after certain duration in node.js?
问题描述
我试图从他们的 url(s) 下载大量图像,然后在 Node.js 中创建一个 PDF 文件.我正在使用 image-downloader 模块下载承诺链中的图像和然后一旦所有承诺都得到解决,使用另一个模块,images-to-pdf, 触发 pdf 的创建.
问题是大多数承诺在大约 5-6 秒内立即得到解决,但有一些图像需要花费相当多的时间来下载.我想在等待一段时间后强制触发 PDF 创建.可以吗?
这是代码
var promises = data.map(function(element){返回下载图像(元素).then( ({文件名}) => {console.log("保存到",文件名);返回文件名;})});Promise.all(promises).then(function (data){var fileName = url.slice(url.indexOf("files")+7, url.length-1).replace("/","_")+".pdf";数据.forEach((元素) =>{console.log(元素);});imagesToPdf(数据,文件名).then(console.log(PDF创建完成."));}).catch ((错误) => {控制台错误(错误);});
data.map
中传递的数据是具有以下性质的 JSON 对象 -
您可以使用 Promise.race()
为每个图像承诺添加超时,如果您要解决它,然后您需要使用一些可以测试的标记值(在本示例中我使用 null
)来解析它,以便您知道在处理结果时跳过它.
function timeout(t, element) {返回新的承诺(解决 => {//用 null 解析这个 promisesetTimeout(resolve, t, element);});}var promises = data.map(function(element) {const maxTimeToWait = 6000;return Promise.race([download.image(element).then(({ filename }) => {console.log("保存到", 文件名);返回文件名;}), 超时(maxTimeToWait)]);});
然后,在处理 Promise.all()
结果的代码中,您需要检查 null
并跳过那些,因为那些是超时的.
I am trying to download a large number images from their url(s) and then creating a PDF file out of them in Node.js. I'm using the image-downloader module to download the images in a promise chain and then once all promises are resolved, using another module, images-to-pdf, to trigger the creation of the pdf.
The problem is that most of the promises get resolved immediately in around 5-6 seconds, but there are a few images that are taking quite a bit of time to download. I'd like to force trigger the PDF creation after a certain interval of waiting. Can that be done?
Here is the code
var promises = data.map(function(element){
return download.image(element)
.then( ({filename}) => {
console.log("Saved to ",filename);
return filename;
})
});
Promise.all(promises).then(function (data){
var fileName = url.slice(url.indexOf("files")+7, url.length-1).replace("/","_")+".pdf";
data.forEach(
(element) => {console.log(element);
});
imagesToPdf(data, fileName)
.then(console.log(" PDF creation done."));
})
.catch ((error) => {
console.error(error);
});
The data getting passed in data.map
is a JSON object of the following nature -
[
{
url:"https://path//to//image//001.jpg",
dest:"local//path//to//image//001.jpg"
},
{
url:"https://path//to//image//002.jpg",
dest:"local//path//to//image//002.jpg"
},
{
url:"https://path//to//image//003.jpg",
dest:"local//path//to//image//003.jpg"
},
...
}]
You can use Promise.race()
to add a timeout to each images promise and if you're going to resolve it, then you need to resolve it with some sentinel value (I use null
here in this example) that you can test for so you know to skip it when processing the results.
function timeout(t, element) {
return new Promise(resolve => {
// resolve this promise with null
setTimeout(resolve, t, element);
});
}
var promises = data.map(function(element) {
const maxTimeToWait = 6000;
return Promise.race([download.image(element).then(({ filename }) => {
console.log("Saved to ", filename);
return filename;
}), timeout(maxTimeToWait)]);
});
Then, in your code that processes the results from Promise.all()
, you need to check for null
and skip those because those are the ones that timed out.
这篇关于如何在 node.js 中的特定持续时间后强制解决承诺?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!