-2

I am working on a project using AngularJS. We are looking at using the revealing module pattern so we can have private functions in our controllers and services. Everything works as expected, except for promises/async data.

Here is an example:

export function SomeController( angularDependencies ) {
  'ngInject' 

  const path = $stateParams.path, // this works fine
        data = someService.getPromise(path) // this should return a promise

  return {
    path,
    data // returns {} instead of a promise
  }
}

My question is this: Is there a way to only do the return after I have all promises resolved? Something like this:

Promises.all(allPromises).then((res) => {
  return { 
    path,
    'data': res
  }
})

I'm open to using $q, rxjs, underscore, whatever to get this to work.

UPDATE: I changed data to this:

const path = $stateParams.path,
      data = Promise.resolve(someService.getPromise(path))

And now I have an object that shows up in Angular's scope, which is progress, but it's a weird object. All of the data that I want is inside of an object, _v.

So I think I am heading in the right direction, but I'll update this if I find anything new.

A Hobbit
  • 62
  • 7
  • no... you can't wait on a promise to resolve before you return. that is simply not possible. Somewhere, you will have to deal with using a callback to access the data. – Kevin B Apr 20 '17 at 17:55
  • @KevinB How would I implement that? Any resources I should look at? – A Hobbit Apr 20 '17 at 18:06
  • promise tutorials. – Kevin B Apr 20 '17 at 18:06
  • @KevinB I'm familiar with promises, but I haven't come across anything that addresses this use case. If you have a tutorial in mind that shows will help, then it's not something that comes up on the first dozen pages of google search and i would love to see it. – A Hobbit Apr 20 '17 at 18:20
  • because... it isn't possible to do what you want to do. Follow a tutorial. – Kevin B Apr 20 '17 at 18:20
  • @KevinB You're saying it's impossible to resolve a promise and return the value, I find that hard to believe. – A Hobbit Apr 20 '17 at 18:21
  • 1
    Yes, [that is very much impossible in javascript](http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call). – Kevin B Apr 20 '17 at 18:21

1 Answers1

2

Here is what I ended up doing.

export function SomeController( angularDependencies ) {
  'ngInject' 

  const path = $stateParams.path, // this works fine
    data = someService.getPromise(path) // returns a promise

  return {
    path,
    data // sends back a promise
  }
}

So once the controller returns the promise, I used a filter to load it into the html: angular1-async-filter

This is how a promise is dealt with in Angular 2+, I just didn't have a good way to deal with it in AngularJS.

Now I can do {{ someAsyncData | async }} and I get the results I wanted.

So the answer to this question is that I couldn't resolve the promise and then return it as part of my module, but I could find a way to handle the promise outside of the module.

A Hobbit
  • 62
  • 7