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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T02:03:05+00:00 2026-06-02T02:03:05+00:00

I am following up to this question , where there is an asynchronous request

  • 0

I am following up to this question, where there is an asynchronous request to draw a route from point A to B, called setDirections. In the end, we want an endpoint to be at the center, but by default the map centers to show most of the route. Simply adding setCenter(endpoint) after setDirections does not result in centering at the endpoint.

I reasoned by using a setTimeout, that setDirections seems to be another asynchronous function, because after waiting a bit, the map centers as I wish.

So how can I ensure my setCenter runs only after setDirections is done? I thought about callbacks but it is not doing what I want.

In my code I did the following, using patterns from Recurial and JavaScript Callback after calling function:

  1. wrap the existing directions drawing code with function wrapper (callback) {
  2. the directions request and drawing is asynchronous, so inside the successful response block, I call
    callback(this.marker.getPosition())
  3. after defining the wrapper, I call wrapper(function(latLng) { map.setCenter(latLng) })

Here’s a JSFiddle. http://jsfiddle.net/EeXQF/2/

The setTimeout “solution” is there, just needs to be uncommented.

  • 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-02T02:03:05+00:00Added an answer on June 2, 2026 at 2:03 am

    (to be honest I dont really follow what you asking, I guess there is a lot of background to your question, which I just dont have. )

    But, one thing to note, Javascript is generally single threaded. Only one bit of code is getting executed at once.

    So your code asks for something to happen. But it can’t generally happen, while your code is still running. The browser will execute it when your code has finished. Actions you ‘start’ will get added to a queue, and run later.

    setTimeout, adds the callback onto the end of the queue. So main function will finish. maps will get a chance to do its stuff (it has stuff in the queue), then your callback will fire.

    (its more complicated than that in reality, but might help explain what you seeing)

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

Sidebar

Related Questions

Following on from this question: Run WCF ServiceHost with multiple contracts Are there any
Following on from this question , I now want to know how to stop
Following this question: Good crash reporting library in c# Is there any library like
This is kind of a soft question, but in the following code, there's a
Following this question, it seems that it is possible to open a file from
Following on from this question what would be the best way to write a
Following up from this question: How can I unlock a file that is locked
Following on from this question I now have code that can attach to a
Following this question , I'd like to know if there is a way to
Following on from this question: Display ALL categories that a product belongs to in

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.