Web Bot Tutorial - Auto Post to Craigslist - 4/4 - Modules and Asynchronous Code with Nightmare.js

preview_player
Показать описание


Javascript is asynchronous meaning that different task can execute at the same time. Normally this a good thing but when you are trying to get a web bot to do things in a specific order its pretty awful. The typical way of dealing with would be to use call backs. But if you do that….it crashes.
So it turns out that nightmare actually uses promises to perform all its actions in a “synchronous” manner. So when you run a nightmare instance and assign those to a function what happens is that it “promises” to do everything youve told it to do. However, when you run it in a loop - nightmare promises to do the work, then the loop accepts the promise as valid AND moves on to the next task on its list. If that task is another nightmare instance then the two nightmare instances try to use the same browser at the same time. Both nightmare instances give up on life and crash your program.

Theres a couple way we can deal with this. However, the one I like best is generators. Generators are a new feature in javascript. You can think of a generator as a “pauseable” function. This makes your javascript behave the way you would expect code to execute in almost every other programming language.
Рекомендации по теме
Комментарии
Автор

Hi love your video, keep making them you are doing much good in the world of JS newbies like us, and don't worry we'll catch on with a bit of practice ha ha :)

wanyueelow
Автор

Nice to have a visual example. That's what missing on official page. Several examples.

ifudiscusswithmeurprobably
Автор

hi powerslacker I really like your videos, one question is it possible to print/get a variable from a script executing in a webpage ?

icroswings
welcome to shbcf.ru