Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RSVP doesn't work right with 'await' #500

Closed
boris-petrov opened this issue Sep 27, 2017 · 5 comments
Closed

RSVP doesn't work right with 'await' #500

boris-petrov opened this issue Sep 27, 2017 · 5 comments

Comments

@boris-petrov
Copy link

I've been struggling with an issue we have for the past few days. It seems to go away when I don't use await with RSVP.all/hash. I still cannot create a reproduction (will post it here if I manage) but we do something like:

class Foo
  constructor: ->
    @loading = {}
    @loading.c = @find()
    @wait()

  findHelper: (properties) ->
    result = for property in properties
      promise = new Ember.RSVP.Promise (resolve) ->
        setTimeout ->
          resolve()
        , 2000
      @loading[property] = promise.then ->
        console.log 'resolved'
    Ember.RSVP.all(result)

  find: ->
    a = @findHelper([])
    b = @findHelper(['a', 'b'])
    await Ember.RSVP.hash({ a, b })
    Ember.RSVP.resolve()

  wait: ->
    await Ember.RSVP.hash(@loading)
    console.log 'done awaiting'

The issue in our case is that done awaiting is printed before resolved. In this test case it doesn't but in our code it does. :) That is, when the wait method is called, @loading contains the two promises that were set in findHelper but awaiting them doesn't actually do that.

If we change the 2 awaits to .thens then it works fine. Can you see anything wrong that we should not be doing?

Another question - awaiting a promise returns a native promise rather than an RSVP one. This is in the context of an Ember app. Can this be changed somehow?

@sukima
Copy link

sukima commented Oct 3, 2017

No, in an ember app it requires an RSVP promise because RSVP is monkey patched to handle an ember run loop. Native promise do not have this. There is history concerning this at emberjs/rfcs#175 but unfortunately it is unlikely to have a solution.

Good news is that there is a very usable and helpful workaround with the ember-co addon which works great for this very use case.

@stefanpenner
Copy link
Collaborator

Another question - awaiting a promise returns a native promise rather than an RSVP one. This is in the context of an Ember app. Can this be changed somehow?

I don't believe this can be changed currently.


@boris-petrov can you provide a runnable example? I will gladly debug if one is provide

@boris-petrov
Copy link
Author

boris-petrov commented Dec 20, 2017

@stefanpenner - about the first thing - isn't doing a:

import { Promise } from 'rsvp'

export default
  name: 'promise'
  initialize: ->
    window.Promise = Promise

Doing exactly that?

About the second thing - I really tried hard reproducing the problem but could not find a short version (or one that I can publish). :( I currently don't have the time to try again. If you prefer, you could close the issue and I will create a new one if/when I manage with a reproduction. Or hopefully the problem just goes away by itself.

@stefanpenner
Copy link
Collaborator

import { Promise } from 'rsvp'

export default
name: 'promise'
initialize: ->
window.Promise = Promise
Doing exactly that?

I don't believe so, not with Native AsyncAwait at-least (potentially though with polyfills)

@stefanpenner
Copy link
Collaborator

If you prefer, you could close the issue and I will create a new one if/when I manage with a reproduction. Or hopefully the problem just goes away by itself.

Unfortunately I think I will need to. I tried some experiments and was also unable to find an issue...
I will gladly reopen if I can get further info/reproduction, but currently I am not sure what I can do. :(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants