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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T20:58:55+00:00 2026-06-15T20:58:55+00:00

ive been trying to use node.js to iterate through an array of cities and

  • 0

ive been trying to use node.js to iterate through an array of cities and make an iterative request to google for directions on each (i then JSON.parse to abstract the drive times). I need to find a way to do this synchronously as otherwise i will just be requesting all the info from google on each city at once. I found a good pattern to use at http://tech.richardrodger.com/2011/04/21/node-js-%E2%80%93-how-to-write-a-for-loop-with-callbacks/ but cannot get the callback to work. As you can see, im using a ‘show’ function to test the same. My code is as follows:

var request = require('request');
var fs = require('fs');
var arr = ['glasgow','preston','blackpool','chorley','newcastle','bolton','paris','york','doncaster'];
//the function I want to call on each city from [arr]
function getTravelTime(a, b,callback){
 request('https://maps.googleapis.com/maps/api/directions/json?origin='+a+'&destination='+b+'&region=en&sensor=false',function(err,res,data){
 var foo = JSON.parse(data);
 var duration = foo.routes[0].legs[0].duration.text;
 console.log(duration);
 });
};

function show(b){
 fs.writeFile('testing.txt',b);
};


function uploader(i){
 if( i < arr.length ){
   show( arr[i],function(){
   uploader(i+1);
   });
 }
}
uploader(0)

The problem I have is that only the first city from the array is output and the callback/iteration never proceeds. Any ideas where im going wrong please?
 

  • 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-15T20:58:56+00:00Added an answer on June 15, 2026 at 8:58 pm

    I was also facing issues like this, so I’ve written a recursive callback function which will act as a for loop but you can control when to increment. The following is that module, name as syncFor.js and include this in your program

    module.exports = function syncFor(index, len, status, func) {
        func(index, status, function (res) {
            if (res == "next") {
                index++;
                if (index < len) {
                    syncFor(index, len, "r", func);
                } else {
                    return func(index, "done", function () {
                    })
                }
            }
        });
    }
    
    //this will be your program if u include this module
    var request = require('request');
    var fs = require('fs');
    var arr = ['glasgow', 'preston', 'blackpool', 'chorley', 'newcastle', 'bolton', 'paris', 'york', 'doncaster'];
    var syncFor = require('./syncFor'); //syncFor.js is stored in same directory
    //the following is how u implement it
    
    syncFor(0, arr.length, "start", function (i, status, call) {
        if (status === "done")
            console.log("array iteration is done")
        else
            getTravelTime(arr[i], "whatever", function () {
                call('next') // this acts as increment (i++)
            })
    })
    
    
    function getTravelTime(a, b, callback) {
        request('https://maps.googleapis.com/maps/api/directions/json?origin=' + a + '&destination=' + b + '&region=en&sensor=false', function (err, res, data) {
            var foo = JSON.parse(data);
            var duration = foo.routes[0].legs[0].duration.text;
            callback(); // call the callback when u get answer
            console.log(duration);
        });
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi ive been trying to use System.Security.Cryptography to encrypt and decrypt a file but
Ive been trying to make a scrollable/zoomable app and everything has gone great except
Ive been trying to get sqlite to use an index with a like to
I am trying to use neo4j database with Express (node.js). I've been looking for
I have been trying to make a treeview that looks something like 2001(root) -Student1(node)
Ive been trying to style a submit button using an image. I would use
I've been trying to use mongo with some data imported, but I'm not able
I've been trying to use buildExpressionParser to parse a language, and I almost have
I've been trying to use a List<> of a Model.Person that I have inside
I've been trying to use the setActionView from the ActionBar in ICS Seems like

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.