Based on what I am hearing, yes, it is fact of life that you can't force
execution to wait on the promise from an async call. Resolve is doing this
for you anyway. If createCustomer() is returning a promise, then you can
do the assignment and use the then() function:
var newCust = custFactory.createCustomer();
newCust.then(function(customer){
//do some stuff with customer
console.log( newCust === customer); //true
})
On Sunday, March 16, 2014 8:32:48 AM UTC-6, MattB wrote:
>
> I have a custFactory service with a method called createCustomer that
> creates a customer model. This method uses data fetched from the server to
> initialize the customer model. I would like for the createCustomer method
> to be synchronous, so as to make the method easier to work with. Of course
> the method used to fetch data is asynchronous. I would like the method to
> look like this:
>
> var newCust = custFactory.createCustomer();
>
> I am aware of the resolve property used in routing, but I may want to use
> this service in a directive, in which case there is no resolve mechanism to
> ensure initialization.
>
> Are there any strategies for turning a method that has an asynchronous
> dependency into a synchronous method call? Or is it just a fact of life
> that once you have introduced an async dependency into a method that that
> method needs to propagate the asynchronicity (via a promise or callback).
>
> Sincerely,
>
> MB
>
--
You received this message because you are subscribed to the Google Groups
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.