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 8300765
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T16:40:38+00:00 2026-06-08T16:40:38+00:00

I may just not be understanding the file reader api, but When I try

  • 0

I may just not be understanding the file reader api, but

When I try to run:

for (var i = 0, f1, f2; f1 = files[sorted_index[i]], f2 = files[sorted_index[i+1]]; i=i+2) {
    var file_one;
    var file_two;

    if(f1.size < f2.size){
        file_one = f1;
        file_two = f2;
    } else {
        file_one = f2;
        file_two = f1;
    }

    var file_one_contents;
    var file_two_contents;


  var reader = new FileReader();
  reader.readAsText(file_one);
  reader.onload = readSuccess;

  function readSuccess(evt){
    file_one_contents = evt.target.result;
  }
  var reader = new FileReader();
    reader.readAsText(file_two);
    reader.onload = readSuccess2;

  function readSuccess2(evt2){
    file_two_contents = evt2.target.result;
  }

    console.log(file_one_contents);
    console.log(file_two_contents);

The console log only contains undefined in it.
The goal of the script it two read in two CSVs and take the data from the pair of files and do some computations.

Thanks!

  • 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-06-08T16:40:41+00:00Added an answer on June 8, 2026 at 4:40 pm

    The API is asynchronous. The “success” functions are called when the operation completes, and that won’t be immediate.

    Move your console.log() calls to inside the handler functions.

    edit — If you need to wait to start doing stuff until both files are ready, you can do something like this:

      var countdown = 2;
    
      var reader = new FileReader();
      reader.readAsText(file_one);
      reader.onload = readSuccess;
    
      function readSuccess(evt){
        file_one_contents = evt.target.result;
        countdown--;
        if (countdown === 0) go();
      }
      var reader = new FileReader();
        reader.readAsText(file_two);
        reader.onload = readSuccess2;
    
      function readSuccess2(evt2){
        file_two_contents = evt2.target.result;
        countdown--;
        if (countdown === 0) go();
      }
    

    There are more sophisticated ways to do it, of course, but that simple trick just waits until the counter is zero before calling “go()”, which represents the function that’d work on processing the files.

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

Sidebar

Related Questions

I fear this just may not be possible, but I'm trying to create a
This may sound like a stupid question but I'm a beginner not just to
I may just be doing something wrong but all of my javascript files as
I'm a bit confused by the Lucene.NET API, but, it may just be a
I see answers to similar questions and I'm sure I may just not be
I just realized that i may not be following best practices in regards to
I may not understand what the return statement does(I thought it just returned a
I just started to use doxygen and may not be familiar with all available
I'm trying to implement the Send button , but running into what may just
i having problem understanding what is wrong with my inheritance. I just can not

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.