javascript - Why is this funciton not loading right first in Node.JS? -
i'm writing twitter bot in node.js have function using npm library called "scrapejs" grabs data yahoo finance , works fine, problem though have function loading after code botton runs first , not clue why. had tweeting section of bot working before if im grabbing data web scrape can't have running after tweeting.
here's code:
console.log("the bot starting work."); var sp = require('scrapejs').init({ cc: 2, // 2 concurrent requests delay: 5 * 1 // delay .05 seconds before each request }); sp.load('https://ca.finance.yahoo.com/q?s=cadusd=x') .then(function ($, res) { //$.q("//h3[@class='r']/a").foreach(function(node){ //adding html tags filter data console.log("scraping web..."); $.q("//span[@class='time_rtq_ticker']/span").foreach(function (node) { var res = { title: node.textcontent //always returns { title: 'number'} (when working) } console.log(res); return res; }) }) .fail(function (err) { console.log(err); }) console.log("why part load first??"); // part comes first before function above heres output looks like: https://imgur.com/yjd2fuw
node.js works asynchronously, means rows in code starts 1 after other, , each of them ends when ends - third row will not wait second 1 finish before starting.
since second function heavy, takes time run, more simple console.log(), there last row end first
Comments
Post a Comment