Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7599113
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T22:34:49+00:00 2026-05-30T22:34:49+00:00

Basically I want to loop through existing arrtickers to get each symbol Then read

  • 0

Basically I want to loop through existing arrtickers to get each symbol

Then read the content inside each symbol url and save the content into local dir.

In php, it will print out each ticker and each symbol in step by step.

But in node, the sequences are mess up.

it will print out all the url_optionable first…

then sometimes print console.log(‘path: ‘ + file), sometimes print console.log(“The file was saved!”);

Everytime run through fs.writefile function, sym value is not detected, the saved file is show as msn-.html

for(var v=0;v<arrtickers.length;v++)
{
    var arrticker= arrtickers[v].split('@@');
    var sym= $.trim(arrticker[1]);

    url_optionable= "http://sample.com/ns/?symbol="+sym;

    console.log('url_optionable: ' + url_optionable);

    request({ uri:url_optionable }, function (error, response, body) {
      if (error && response.statusCode !== 200) {
        console.log('Error contacting ' + url_optionable)
      }

      jsdom.env({
        html: body,
        scripts: [
          jqlib
        ]
      }, function (err, window) {

        var $ = window.jQuery;
        var data= $('body').html();

        var file= "msn-"+sym+".html";
        console.log('path: ' + file);

        fs.writeFile(file, data, function(err) {
            if(err) {
            console.log(err);
            } 
            else 
            {
                console.log("The file was saved!");
                }
        });
      });
    });
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T22:34:50+00:00Added an answer on May 30, 2026 at 10:34 pm

    ZeissS is right. Basically, you can’t use variables declared inside the for loop in a callback function to an asynchronous call, because they will all be set to the last value in the loop. So in your code, url_optionable and sym will correspond to arrtickers[arrtickers.length - 1].

    Either use (as ZeissS suggests):

    arrtickers.forEach(function(arrticker) {
        // Get symbol and url, make request etc
    });
    

    Or declare a function which takes sym and does the request, and call that in your loop:

    function getSymbol(symbol) {
        // Request url and read DOM
    }
    
    for(var v=0;v<arrtickers.length;v++) {
        var arrticker = arrtickers[v].split('@@');
        var sym = $.trim(arrticker[1]);
    
        getSymbol(sym);
    }
    

    Personally, I would opt for the forEach solution.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What i basically want to do is to get content from a website and
I have this perl code below, basically want to loop through these 2 files.
Basically I want the scenario outline to loop through all of the various combinations
Basically, what I want to do is have the template system loop through two
Basically I am looping through tweets. I want to pause for 20seconds between each
Basically i got this for loop and i want the number inputed (eg. 123)
I have a 2-dimensional array of objects and I basically want to databind each
I want to loop through all the elements in the DOM structure of a
I'm using a for-each loop to move through a 2d array of chars in
I basically want to use link_to to link to the index method of a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.