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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:28:09+00:00 2026-05-23T11:28:09+00:00

I am trying to make some of my javascript a bit more testable, as

  • 0

I am trying to make some of my javascript a bit more testable, as part of this I am wrapping up certain functionality in classes so I can mock it out in my tests.

Anyway I am trying to wrap up my ajax, so as far as my app would be concerned it is asking a service for an object. Internally it would make an ajax request and then do something with the data and then return it.

So would something like the example below be possible? (I am on the move at the moment so cant try it for myself)

function SomeAjaxService(webServiceUrl)
{
    this.getSomeModel = function(someUniqueId){
        var ajaxOptions = {
            url: webServiceUrl,
            data: {id : someUniqueId},
            Success: function(data) { return new SomeModel(data); }
        };
        $.ajax(ajaxOptions);
    };
}

var ajaxService = new SomeAjaxService("http://someurl");
var myModel = ajaxService.getSomeModel(1);

As ajax is asynchronous by nature, I think you can set it to be synchronous but I just wanted to check what the options were…

  • 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-23T11:28:10+00:00Added an answer on May 23, 2026 at 11:28 am

    Setting it to synchronous is a bad idea as you will lock javascript (even the browser) until the request returns. Your code will not work because the success function is called from a different context to your direct getSomeModel method call. The best approach would be for you to call getSomeModel and pass it a callback function that will be executed when the model is received.

    function SomeAjaxService(webServiceUrl)
    {
        this.getSomeModel = function(someUniqueId, callback){
            var ajaxOptions = {
                url: webServiceUrl,
                data: {id : someUniqueId},
                // calls the callback you passed
                Success: function(data) { callback(new SomeModel(data)); }
            };
            // this is asynchronous and doesn't return anything
            $.ajax(ajaxOptions);
        };
    }
    
    var ajaxService = new SomeAjaxService("http://someurl");
    // your callback down here
    var myModel;
    ajaxService.getSomeModel(1, function(model) {
        myModel = model;
        // do something with model
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to make sure some data is auto-deleted when there's no more references
I'm trying to make it working for quite some time,but just can't seem to
I'm trying to make a loading overlay thing. Currently, I have some javascript adding
I am trying to make my pagination script a bit more dynamic and am
I'm trying to make some tests on a JavaScript application and someone advised me
I'm currently trying to make some ajax post between cross-domains by following this tutorial
I am trying to apply some javascript to a pdf to make it print
I'm trying to make some types in Django that map to standard Django types.
I'm trying to make some simple XML with Java and org.w3c.dom, but I got
I'm trying to make some text bold using HTML, but I'm struggling to get

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.