Queue of node-horseman calls

49 Views Asked by At

I'm developing a web scraper (in its early stages) and I plan to do x horseman calls. This calls are promise based but I want the calls to be sequentially, when one finishes, the next starts.

How can I achieve this behavior?

1

There are 1 best solutions below

0
Horia Coman On BEST ANSWER

You can simply do something like:

asyncCall(args[1]).done(() => asyncCall(args[2]))

Here asyncCall is whatever function you need to call. It takes some arguments and returns a Promise.

So you are basically waiting for the result of one call and then invoking the second call.

However, this can get pretty unwieldy when there's a lot of calls to make. If you are using async/await, which you should, given that you're working on a new project, you can code it much better like:

await asyncCall(args[1]);
await asyncCall(args[2]);
...

Or, even better:

for (let arg of args) {
    await asyncCall(arg)
}