3

I would like to chain resource in AngularJS.

The goal is that resource B wait the end of ressource A before the launch.

I tried this:

var p = $q.when(true);
p = p.then(function() {
    return $resource(...).query().$promise;
});
p = p.then(function() {
    return $resource(...).query().$promise;
});
p = p.then(function() {
    return $resource(...).query().$promise;
});

The point is that the call is not synchronous.

1 Answer 1

2

There is a way to group many asynchronous calls using $q.all. The docs for this are here.

$q.all() will allow you to send in all your queries and then wait for them all the return before resolving a combined promise. However, if you want your queries to happen in a specific order, this may not work.

Hope this helps.

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

2 Comments

This is working fine with $q.allbut are you sure that query return a promise. It's not clear in my mind. "It is important to realize that invoking a $resource object method immediately returns an empty reference (object or array depending on isArray). Once the data is returned from the server the existing reference is populated with the actual data."
On a second read of the docs, I think you are right to use $promise. I'll change my answer a bit.

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.