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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T04:35:44+00:00 2026-05-24T04:35:44+00:00

In my view I want: :coffeescript Gmap(‘#canvas’).getAddressBounds request.term which is defined in maps.js.coffee as

  • 0

In my view I want:

:coffeescript
  Gmap('#canvas').getAddressBounds request.term

which is defined in maps.js.coffee as

Gmap = (mapId) ->
  getAddressBounds: (address) ->
    data = []
    $(mapId).gmap3
      action: 'getAddress'
      address: address
      callback: (results) ->
        return unless results
        data = $.map results, (item) ->
          bounds: item.geometry.bounds
    data

This doesn’t work though. First, there’s a scope issue. The Gmap function is not visible to the script in the view. If I add the code directly to the view, Gmap is visible, but data always returns as [].

  • 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-24T04:35:44+00:00Added an answer on May 24, 2026 at 4:35 am

    What’s going on is that you’re treating asynchronous code as if it’s synchronous. Some debug output might help you visualize this:

    Gmap = (mapId) ->
      getAddressBounds: (address) ->
        data = []
        console.log '1: Calling gmap3'
        $(mapId).gmap3
          action: 'getAddress'
          address: address
          callback: (results) ->
            console.log '3: Callback called'
            return unless results
            data = $.map results, (item) ->
              bounds: item.geometry.bounds
        console.log '2: Returning data'
        data
    

    When you pass a callback, it could get called at any time. If it were called during the gmap3 function, then data would indeed be set before being returned. But the reason gmap3 uses a callback to return its result, rather than just returning, is that the function is asynchronous—in particular, it calls the callback when the server responds to your query. The way that JavaScript does events, that means your callback is guaranteed not to be called until after all code has finished executing.

    There’s no way to wrap an asynchronous function in a synchronous one in JavaScript (or CoffeeScript); even running an infinite loop until your callback is called wouldn’t work, because, again, the JS runtime doesn’t handle events like server responses (or even user input events) until all code has finished executing. So all you can do is change your function, too, to use a callback:

    Gmap = (mapId) ->
      getAddressBounds: (address, cb) ->
        $(mapId).gmap3
          action: 'getAddress'
          address: address
          callback: (results) ->
            return unless results
            cb $.map results, (item) ->
              bounds: item.geometry.bounds
    

    Then call it like so:

     Gmap('#canvas').getAddressBounds request.term, (data) -> console.log data
    

    I talk a little bit more about the JS event model in my CoffeeScript book. Also, John Resig’s How JavaScript Timers Work is a must-read. Asynchronicity takes some getting used to, but the benefits over multithreading are spectacular.

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

Sidebar

Related Questions

On each cell of my table view I want to have a bar that
i want a view with UITabBAr at The bottom with some buttons+Navigation controller on
I want to view the nfs shares that are exported on a windows server
I have a view that I want to add some custom drawing to. I
Using Eclipse I want to view the source code for a core Java class
I have an index view that I want to update automatically as the user
I have a view that I want to be converted into JSON. What is
I have a partial view that I want to basically take care of itself
I have a MS SQL view that I want to make available as a
my application consist of a landscape view where i want to put a modalview

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.