0

I don't familiar with JavaScript Promises function.

Currently I have this code on my Angular Controller

$http.get('pages/about.html').then(function(response) {
    var raw_html = response.data;
    $scope.aboutHTML = raw_html.replace(/</g,"&lt;");
});

I want to re-write the code so I could do something like this

$scope.indexHTML = getHTML('pages/index.html');
$scope.aboutHTML = getHTML('pages/about.html');
...

with function like this

function getHTML(url){
    $http.get(url).then(function(response) {
        var raw_html = response.data;
        return = raw_html.replace(/</g,"&lt;");
    });
}

How to write the the code properly for the function above?

[Update #1]

Temporary Solution by @charlietfl

function getHTML(url){
    // return the promise
    return $http.get(url).then(function(response) {
        var raw_html = response.data.replace(/</g,"&lt;");
        return  raw_html;
    });
}

getHTML('pages/index.html').then(function(raw_html){
    $scope.indexHTML = raw_html;
});

I wanna to write this function to reduce the manual work, with this way I still need to write down $scope.{page} for each page, so anyone know better way?

[Update #2]

Solution by @joeytwiddle

function getHTML(url){
    // return the promise
    return $http.get(url).then(function(response) {
        var raw_html = response.data.replace(/</g,"&lt;");
        return  raw_html;
    });
}

getHTML('pages/index.html').then(function(raw_html){
    $scope.indexHTML = raw_html;
});
1

2 Answers 2

1

There is no way to just return the result, because the result will not be available until some time in the future. #asynchronous

You can only handle the result using a callback function.

If you want to minimize the work from outside, I would suggest something like this:

getHTMLAndStore('pages/index.html', $scope, 'indexHTML');
getHTMLAndStore('pages/about.html', $scope, 'aboutHTML');

function getHTMLAndStore(url, object, property) {
    $http.get(url).then(function(response) {
        var raw_html = response.data;
        var weird_html = raw_html.replace(/</g,"&lt;");
        object[property] = weird_html;
    }).catch(console.error.apply(console));
}

This is pure JS and not really related to Angular.

Note that these two requests will fire in parallel, not in sequence.

Sign up to request clarification or add additional context in comments.

Comments

1

$http returns a promise so you need to return that promise from the function and use another then() to assign the scope variable:

function getHTML(url){
    // return the promise
    return $http.get(url).then(function(response) {
        var raw_html = response.data.replace(/</g,"&lt;");
        return  raw_html;
    });
}

getHTML('pages/index.html').then(function(raw_html){
    $scope.indexHTML = raw_html;
});

Currently your function doesn't return anything

1 Comment

No... You can't return an asynchronous data response from a function

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.