Returning a promise as value as the same time (like in restangular)?

363 views
Skip to first unread message

Ryan Zec

unread,
Apr 22, 2013, 8:19:54 PM4/22/13
to ang...@googlegroups.com
Restangular shows two examples where is one case:

// This will query /accounts and return a promise. As Angular supports setting promises to scope variables
// as soon as we get the information from the server, it will be shown in our template :)
$scope.allAccounts = baseAccounts.getList();

And other shows:

//Here we use Promises then 
// GET /accounts
baseAccounts.getList().then(function (accounts) {

How can the same call return a value in one case but a promise in another?

I am asking as a situation has come up where that might come in handy.  I looked at the restangular source code however it seem this function other return a promise, not data.

bkc

unread,
Apr 22, 2013, 9:29:49 PM4/22/13
to ang...@googlegroups.com
I suspect that  baseAccounts.getList() is returning a result from $resource, which has a .$then property on it.

That property comes from the underlying $http service

I do something similar where the get_quotes function is doing a $resource.query and returning that

        @scope.quote_list = @get_quotes @D2DService.orgid(), '608500'
        @scope.quote_list.$then () =>
            @scope.shipment_data = @extract_units_from_quotes @scope.quote_list


Note I am referencing property '$then' but your example shows just 'then', I'm not sure where that comes from, might be  a restangular thing.

Gonto

unread,
May 23, 2013, 6:14:45 PM5/23/13
to ang...@googlegroups.com
Hey,

Restangular does indeed return a promise when you do baseAccounts.getList().

The thing is that Angular's HTML $watch for the template know how to work with Promises. So, while the promise is not resolved, you won't get anything shown in the HTML. As soon as it's resolved, Angular calls the then by itself and then renders the data, which is quite awesome :).

The only thing I've added are "enhanced promises".

It means that if you do accounts.getList().get('length'), it'll return the promise of the list, to a promise of a long value for the length :).

Bests,
Gonto
Reply all
Reply to author
Forward
0 new messages