Home Ask Login Register

Developers Planet

Your answer is one click away!

CorgJR February 2016

Why is this funciton not loading right first in Node.JS?

I'm writing a twitter bot in Node.JS but I have a function that is using an npm library called "scrapejs" it grabs the data from yahoo finance and works just fine, problem is though I have the function loading after the code from the botton runs first and not a clue as to why. I had my tweeting section of the bot working before but if Im grabbing data from the web scrape I can't have that running after tweeting.

Here's the code:

console.log("The Bot Is Starting To Work.");

var sp = require('scrapejs').init({
    cc: 2, // up to 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 the HTML Tags to filter the data
        console.log("Scraping The 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 does this part load first??"); // This part comes first before the function above

Heres what the output looks like: https://imgur.com/YJD2FUW

Answers


Nir Levy February 2016

node.js works asynchronously, this means that the rows in your code starts one after the other, and each of them ends when it ends - the third row will not wait for the second one to finish before starting.

Since your second function is heavy, it takes some time to run, much more than a simple console.log(), there for the last row will end first


Aminadav February 2016

For making your life easier you can use deasync module.

https://github.com/abbr/deasync

  • Just install it using: npm i -s deasync

Then you can convert any async function to sync one.

Post Status

Asked in February 2016
Viewed 3,018 times
Voted 10
Answered 2 times

Search




Leave an answer


Quote of the day: live life