admin管理员组文章数量:1357678
I need to execute unknown number of http requests in a node.js program, and it needs to happen synchronously. only when one get the response the next request will be execute. How can I implement that in JS?
I tried it synchronously with the requset package:
function HttpHandler(url){
request(url, function (error, response, body) {
...
})
}
HttpHandler("address-1")
HttpHandler("address-2")
...
HttpHandler("address-100")
And asynchronously with request-promise:
async function HttpHandler(url){
const res = await request(url)
...
}
HttpHandler("address-1")
HttpHandler("address-2")
...
HttpHandler("address-100")
Non of them work. and as I said I can have unknown number of http request over the program, it depends on the end user.
Any ideas on to handle that?
I need to execute unknown number of http requests in a node.js program, and it needs to happen synchronously. only when one get the response the next request will be execute. How can I implement that in JS?
I tried it synchronously with the requset package:
function HttpHandler(url){
request(url, function (error, response, body) {
...
})
}
HttpHandler("address-1")
HttpHandler("address-2")
...
HttpHandler("address-100")
And asynchronously with request-promise:
async function HttpHandler(url){
const res = await request(url)
...
}
HttpHandler("address-1")
HttpHandler("address-2")
...
HttpHandler("address-100")
Non of them work. and as I said I can have unknown number of http request over the program, it depends on the end user.
Any ideas on to handle that?
Share Improve this question edited Jan 22, 2021 at 22:52 dev3dev asked Jan 22, 2021 at 22:24 dev3devdev3dev 1581 gold badge3 silver badges10 bronze badges 7- 1 There is no such thing as synchronous http requests in nodejs (all networking in nodejs is asynchronous). Do you perhaps mean you need to run them in sequence one after another, even though they are asynchronous? And, please show what you're trying to do with the responses from these multiple calls as that will effect how the code should be written. Right now, they don't seem dependent upon one another so they could be run in parallel, without sequencing them in a strict order. – jfriend00 Commented Jan 22, 2021 at 22:26
-
2
Also, the
request()
package has been deprecated and is not remended for new code. You can pick from a list of alternatives here. My favorite isgot()
which will also make it easier to sequence your calls by using promises andawait
. – jfriend00 Commented Jan 22, 2021 at 22:28 - Please don't even try – Aluan Haddad Commented Jan 22, 2021 at 22:31
- @jfriend00 The http requests can be asynchronous, I'm fine with that. I just need the program to be synchronous. for example to get notify when one http got a response and then send the next one, I guess I can use some form of callback, but I'm not sure how to implement that, since I don't have a list of all the requests before runtime. It will be totally dynamic. – dev3dev Commented Jan 22, 2021 at 22:44
- Please show the real logic of your problem so we can offer a plete solution to the problem. – jfriend00 Commented Jan 22, 2021 at 22:52
1 Answer
Reset to default 4Use the got()
library, not the request()
library because the request()
library has been deprecated and does not support promises. Then, you can use async/await
and a for
loop to sequence your calls one after another.
const got = require('got');
let urls = [...]; // some array of urls
async function processUrls(list) {
for (let url of urls) {
await got(url);
}
}
processUrls(urls).then(() => {
console.log("all done");
}).catch(err => {
console.log(err);
});
You are claiming some sort of dynamic list of URLs, but won't show how that works so you'll have to figure out that part of the logic yourself. I'd be happy to show how to solve that part, but you haven't given us any idea how that should work.
If you want a queue that you can regularly add items to, you can do something like this:
class sequencedQueue {
// fn is a function to call on each item in the queue
// if its asynchronous, it should return a promise
constructor(fn) {
this.queue = [];
this.processing = false;
this.fn = fn;
}
add(...items) {
this.queue.push(...items);
return this.run();
}
async run() {
// if not already processing, start processing
// because of await, this is not a blocking while loop
while (!this.processing && this.queue.length) {
try {
this.processing = true;
await this.fn(this.queue.shift());
} catch (e) {
// need to decide what to do upon error
// this is currently coded to just log the error and
// keep processing. To end processing, throw an error here.
console.log(e);
} finally {
this.processing = false;
}
}
}
}
本文标签: nodejsHow to execute synchronous HTTP requests in JavaScriptStack Overflow
版权声明:本文标题:node.js - How to execute synchronous HTTP requests in JavaScript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744080660a2587575.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论