0

I have never looked at angular before and I am working on a project that uses it extensively, I am quite confused by the promise api code.

If a function returns promise.then (which I gather is called when the promise is fulfilled) is this the result of the promise function? Is there any blocking when you do this as the result isnt immediately available?

A crude example:

$scope.refreshFilters = function (filters) {
    var promise;
    promise = getConfig(filters);
    return promise.then(function (data) {
        return data.availableContent;
    });
};

Do calls to the refreshFilters function need to block now because the result isnt available immediately?

2 Answers 2

2

Correct, promise.then(...) itself returns a promise. You can find the Promise A+ spec, which $q mostly follows here: https://promisesaplus.com/

A promise is a way of handling asynchronous work. In your example, getConfig(filters) returns a promise, which is handled with the .then(...) call. And refreshFilters returns a promise because getConfig(filters).then(...) also returns a promise. If getConfig executes asynchronously, then refreshFilters will as well.

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

Comments

2

The return inside the "then" returns a promise not the plain value, so

return promise.then(function (data) {                    
                return data.availableContent;
            });
};

is actually returning a promise.

1 Comment

... which resolves to the value returned by the function supplied to then()

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.