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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T17:20:13+00:00 2026-05-28T17:20:13+00:00

Is there an elegant way to asynchronously map an object or array in coffeescript?

  • 0

Is there an elegant way to asynchronously map an object or array in coffeescript? (Or javascript.)

Imagine I have some things in an object:

things = 
  x: 
    ...
  y:
    ...
  z:
    ...

thingCount = 3

I want to create a method that will process each of these things and return the processed object. The process has to make an asynchronous call to fetch some info about each thing.
At first I tried just to loop through the properties like so:

processThings = (callback) ->

  processedThings = {}
  count = 0

  for key,val in things

    asyncJob key,val (err,result) ->

      if err
        callback error
      else
        # PROBLEM: key has the incorrect value here
        processedThings[key] = result      
        count += 1
        if count == thingCount
          callback null,processedThings

The problem is that the value of key changes in the loop. So my solution is to create a sub-function so that the key variable is contained within its closure:

processThings = (callback) ->

  processedThings = {}
  count = 0

  processThing = (key,val) ->

    asyncJob key,val (err,result) ->

      if err
        callback error
      else
        processedThings[key] = result      
        count += 1
        if count == thingCount
          callback null,processedThings

  processThing key,val for key,val of things

But boy howdy that sure is fugly. Is there a preferred pattern for this?

  • 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-28T17:20:14+00:00Added an answer on May 28, 2026 at 5:20 pm

    CoffeeScript covers this with the do keyword, described at the end of Loops and Comprehensions:

    When using a JavaScript loop to generate functions, it’s common to insert a closure wrapper in order to ensure that loop variables are closed over, and all the generated functions don’t just share the final values. CoffeeScript provides the do keyword, which immediately invokes a passed function, forwarding any arguments.

    for filename in list
      do (filename) ->
        fs.readFile filename, (err, contents) ->
          compile filename, contents.toString()
    

    It can be applied to your code as:

    processThings = (callback) ->
    
      processedThings = {}
      count = 0
    
      for key,val in things
    
        # add this to close over `key`
        do (key) ->
    
          asyncJob key,val (err,result) ->
    
            if err
              callback error
            else
              processedThings[key] = result      
              count += 1
              if count == thingCount
                callback null,processedThings
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there an elegant way to create a CLLocationCoordinate2D object from an array of
Is there an elegant way to have performant, natural sorting in a MySQL database?
Is there some elegant way to add an empty option to a DropDownList bound
Is there any elegant way to exploit the correct spacing feature of print numpy.array
I'm trying to find an elegant way in Coffeescript to merge an array of
Is there more elegant way of escaping array elements: mysql_query(SELECT * from mytable WHERE
Is there more elegant way(LINQ or other) to initialize array like this? int[] result
Is there an elegant way to use a specific dependency as a file object
Is there an elegant way of getting values from a massive multi-dimensional array using
Is there an elegant way to turn an array of primitives into an array

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.