I am writing a helper class in JS for my web site that goes to the server and pulls back a fresh guid. I made this a helper function because I plan to use this functionality often on my site. The complication I am having is in writing a method that will return the string as a synchronous operation rather than return the promise as an asynch.
Here is the code I have so far:
//create a namespace for our helpers, just in case
var littleMouse = littleMouse || {};
//Class definition
littleMouse.Helper = function () { };
//method to request a new GUID from the server
littleMouse.Helper.prototype.newGuid = function () {
return $.get("/api/Helper/Guid")
.done(function (response) {
console.log(response);
return response;
});
};
I want to be able to use code like this:
var myHelper = new littleMouse.Helper();
var myGuid = myHelper.newGuid();
alert(myGuid);
When I do this, myGuid is an object and not a string. And I have confirmed that -- of course -- my alert method is triggered before the console has logged the string, so the object being returned is probably the request / promise.
How do I update my code so that the newGuid() method is invoked synchronously and returns back the actual string?