admin管理员组文章数量:1401176
I'm using node-fetch to make a HEAD request to an Amazon bucket to check the status of a file, but I am trying to set up the module P-Queue to give me control over concurrency.
I have an array, resources
, that contain all of my data including file paths to check within that bucket. Then map the array like so:
resources.map((resource) => {
queue
.add(validateImage(resource))
.catch((err) => console.error(`Failed to add resource to queue: ${err}`));
});
And this is what my validateImage
function looks like:
const validateImage = (resource) => {
const path = `https://${bucketUrl}/${env}/${resource.path}`;
verbose && console.log(`Fetching resource: ${path}`);
return fetch(
path,
{
method: 'HEAD',
timeout
}
)
.then(res => {
validatedResources.push({
...resource,
statusCode: res.status
});
})
.catch(err => {
validatedResources.push({
...resource,
error: err.type
});
});
}
But whenever I run this script I get the error Failed to add resource to queue: TypeError: fn is not a function
as many times as I have resources.
I've checked that what is being returned from validateImage is a Promise and I also tried rewriting the example from the p-queue github to use fetch (suspecting that being the reason):
const PQueue = require('p-queue');
const fetch = require('node-fetch');
const queue = new PQueue({concurrency: 1});
queue.add(() => fetch(
''),
{ method: 'HEAD'}
).then(() => {
console.log('Done: sindresorhus');
});
queue.add(() => fetch('')).then(() => {
console.log('Done: ava.li');
});
This does work, so I'm not really sure where to look anymore?
I'm using node-fetch to make a HEAD request to an Amazon bucket to check the status of a file, but I am trying to set up the module P-Queue to give me control over concurrency.
I have an array, resources
, that contain all of my data including file paths to check within that bucket. Then map the array like so:
resources.map((resource) => {
queue
.add(validateImage(resource))
.catch((err) => console.error(`Failed to add resource to queue: ${err}`));
});
And this is what my validateImage
function looks like:
const validateImage = (resource) => {
const path = `https://${bucketUrl}/${env}/${resource.path}`;
verbose && console.log(`Fetching resource: ${path}`);
return fetch(
path,
{
method: 'HEAD',
timeout
}
)
.then(res => {
validatedResources.push({
...resource,
statusCode: res.status
});
})
.catch(err => {
validatedResources.push({
...resource,
error: err.type
});
});
}
But whenever I run this script I get the error Failed to add resource to queue: TypeError: fn is not a function
as many times as I have resources.
I've checked that what is being returned from validateImage is a Promise and I also tried rewriting the example from the p-queue github to use fetch (suspecting that being the reason):
const PQueue = require('p-queue');
const fetch = require('node-fetch');
const queue = new PQueue({concurrency: 1});
queue.add(() => fetch(
'http://sindresorhus.'),
{ method: 'HEAD'}
).then(() => {
console.log('Done: sindresorhus.');
});
queue.add(() => fetch('http://ava.li')).then(() => {
console.log('Done: ava.li');
});
This does work, so I'm not really sure where to look anymore?
Share Improve this question asked Jul 21, 2018 at 14:59 zkwskzkwsk 2,1366 gold badges31 silver badges35 bronze badges 01 Answer
Reset to default 7queue.add
expects a function, however you are passing a Promise.
When you call queue.add(validateImage(resource))
, the engine first evaluates validateImage(resource)
, which returns a Promise. Then it evaluates queue.add(<returned promise>)
.
Simply change the call to an arrow function: queue.add(() => validateImage(resource))
本文标签: javascriptQueueing Promises with PQueue node moduleStack Overflow
版权声明:本文标题:javascript - Queueing Promises with P-Queue node module - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744272198a2598237.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论