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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T10:22:46+00:00 2026-06-09T10:22:46+00:00

Longtime listener, first time caller :-) I’m new to nodejs and to javascript and

  • 0

Longtime listener, first time caller 🙂

I’m new to nodejs and to javascript and am struggling to wrap my head around the event & callback model.

My goal: return array of photos (name, width, height, exif data) as JSON. This is needed so that my app can then work its magic with the JSON array of pics.

Situation: my code seems to work except that things are not running the in the order that I’d like. I’m expecting to stream the JSON for each picture to the HTTP Response but my console logging reveals that things aren’t in desired sequence and I suspect that res.end() is being called too early. This means that my web request gets nothing in the response.

The relevant code:

fs.readdir(readDir, function (err, files) {
    var picsInfo;
    if (err) res.writeHead(500, err.message);
    else if (!files.length) res.writeHead(404);
    else {
        res.writeHead(200, { 'Content-Type': 'application/json' });
            files.forEach(function(file) {
        console.log('easyimage');
        easyimg.info(readDir + '/' + file, function(err, stdout, stderr) {
            if (err) throw err;
            console.log(JSON.stringify(stdout));
            res.write(JSON.stringify(stdout));
        });
        });
    }
    res.end();
});

The output I see in my console reveals things are not in order (I’m expecting each photo to be sent to res):

easyimage
easyimage
easyimage
easyimage
easyimage
easyimage
easyimage
easyimage
easyimage
easyimage  
{"type":"JPEG","depth":"8","width":"933","height":"1400","size":"532365B","name":"6.jpg"}
{"type":"JPEG","depth":"8","width":"1400","height":"933","size":"318134B","name":"3.jpg"}
{"type":"JPEG","depth":"8","width":"1400","height":"933","size":"310927B","name":"10.jpg"}
{"type":"JPEG","depth":"8","width":"933","height":"1400","size":"258928B","name":"1.jpg"}
req.method=GET...
{"type":"JPEG","depth":"8","width":"1400","height":"928","size":"384475B","name":"7.jpg"}
{"type":"JPEG","depth":"8","width":"933","height":"1400","size":"469711B","name":"4.jpg"}
{"type":"JPEG","depth":"8","width":"1400","height":"933","size":"392666B","name":"2.jpg"}
{"type":"JPEG","depth":"8","width":"1400","height":"933","size":"354468B","name":"5.jpg"}
{"type":"JPEG","depth":"8","width":"1400","height":"933","size":"438143B","name":"9.jpg"}
{"type":"JPEG","depth":"8","width":"933","height":"1400","size":"304939B","name":"8.jpg"}

I’ve tried using some examples I’ve found (e.g. http://blog.nakedjavascript.com/going-evented-with-nodejs but think that I’m just missing something key in terms of the eventing/callback model. Any pointers or help would be much appreciated. Also, if my approach here (i.e. sending JSON to res, etc.) is dumb or sub-optimal I’d also love some tips on what might be best.

Thanks in advance for any pointers & help!

  • 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-09T10:22:48+00:00Added an answer on June 9, 2026 at 10:22 am

    OK, I can’t run the code, so it might need some tweaks:

    fs.readdir(readDir, function(err, files) {
        var filesPath = files.map(function(file) {
          return readDir + '/' + file;
        });
    
        async.map(filesPath, easyimg.info, function(err, json) {
            if(err)
                return res.end(500, err.message);
    
            res.end(JSON.stringify(json));
        });
    });
    

    What was wrong with your code ?

    • As I said earlier, res.end() was called immediately because easyimg.info answers asynchronously.
    • You call easyimg.info for each file, but there is no guarantee that the response will arrive in order. See the wonderful async lib (npm install async).
    • When you return an error with a callback, make sure you return as well. Always use the pattern return cb(err).
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

long-time listener, first-time caller. I am relatively new to programming and was looking back
Long-time listener, first-time caller. I'm relatively new to XPath and looked at several other
Long time listener, first time caller. I'm a 6 month old dev from Code
long time listener first time caller... I have this function (I don;t recall where
Long time listener, first time caller here. Ok, I apologize for the length of
I’m a long time listener, but first time caller here… Hoping you can help
I am a long time listener first time caller. I am working on a
long time listener, first time caller... I am creating a splash screen derived from
As they say on the radio - long time listener first time caller.... Here's
Long time listener, first time caller. I'm a full time SE during the day

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.