Gaurravs Gaurravs - 1 month ago 9
AngularJS Question

Synchronous Call AngularJS

METHOD 1

{
var myform = getForm();
var myVar = $sce.trustAsHtml(myForm);
}


METHOD 2

var getForm = function () {
var form = "";
//API CALL here using custom service
.then(
function (response) {
form = //processing here
},
function (response) {
}
return form;
};


In below scenario I am calling
getForm()
method and need processed data from form variable. But it always return empty before processing.

How can I make this call sync so that I can
getForm()
return the processed data and returns to
method 1

Answer Source

Deal with promise here:

var myform = getForm().then(
    (form) => {
       $sce.trustAsHtml($sce.valueOf(form))
    },
    (error) => {
       // getForm - fail
    });

METHOD 2 - return promise instead form value

var getForm = function () {
    return //API CALL here using custom service
};

Or even easier:

yourService.serviceMethod().then(
    (form) => {
       $sce.trustAsHtml(form)
    },
    (error) => {
       // getForm - fail
});