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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:12:57+00:00 2026-06-15T16:12:57+00:00

I have a little question as to how to get the below program structure

  • 0

I have a little question as to how to get the below program structure to work in the way that I am intending.

I want it to execute so that the console will print out the numbers in the correct order. Currently when run, it will run so that that methods are run in such a way to result in the console logging out 1 3 2 instead of 1 2 3.

I am guessing that this is just down to a misunderstanding of how javascript handles function calls.

Coffee:

class exports.Class
    constructor: (@options) ->
    onEdit: =>
        @secondFunction()
        console.log "3"

    secondFunction: =>
        console.log "1"
        @externalClass.getData( (callback) =>
            console.log "2"
        ) 

JS:

var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };

exports.Class = (function() {

  function Class(options) {
    this.options = options;
    this.secondFunction = __bind(this.secondFunction, this);

    this.onEdit = __bind(this.onEdit, this);

  }

  Class.prototype.onEdit = function() {
    this.secondFunction();
    return console.log("3");
  };

  Class.prototype.secondFunction = function() {
    var _this = this;
    console.log("1");
    return this.externalClass.getData(function(callback) {
      return console.log("2");
    });
  };

  return Class;

})();
  • 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-15T16:12:58+00:00Added an answer on June 15, 2026 at 4:12 pm

    What’s happening is that when you call onEdit() it immediately (and synchronously) calls secondFunction(). secondFunction given that was called synchronously will print “1” immediately and make the call to get data. Your callback won’t be called until a later time. You can imagine that it has been put on a queue for execution. Meanwhile, secondFunction will end, return execution to onEdit and the last line will output “3”.

    You have no control on when the “2” will be displayed. It can be a millisecond later or 30 seconds later.

    There are libraries like https://github.com/caolan/async that can help you orchestrate workflows in an asynchronous environment. You should also take a look at the book Async JavaScript http://www.amazon.com/Async-JavaScript-ebook/dp/B007N81FE2 to get a good idea of how to work on this kind of environment.

    You could rewrite your code like this and get the desired effect but this might not be as easy on your real code but it’s an idea:

    class exports.Class
        constructor: (@options) ->
        onEdit: =>
            @secondFunction()
    
        secondFunction: =>
            console.log "1"
            @externalClass.getData( (callback) =>
                console.log "2"
                @lastStep()
            ) 
    
        lastStep: =>
            console.log "3"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a little question I want to click on my smileys and insert
I have a little question to community. Does ASP.NET MVC allow to execute an
I have this little question that's been on my mind for a while now.
I have little bit longer question for you - but hope answer will be
I am trying to work through this question and I have had little success
Okay, So I will get a little more specific (since my last question). Basically,
I will start by saying that this is a 2-part question. I have a
what i have here works exactly how i want it. My question is that
I have a question here that looks a little like some of the ones
i need help with a little code that i have below. i'm a newbie

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.