       pending: true
 
     promise.then (value) =>
-      @safelySetState
+      @safelySetState promise,
         pending: false
         resolved: true
         rejected: false

module.exports = React.createClass
  displayName: 'PromiseRenderer'

  pendingClass: 'promise-pending'

  propTypes:
    promise: React.PropTypes.instanceOf(Promise).isRequired
    then: React.PropTypes.func.isRequired
    catch: React.PropTypes.func

  getDefaultProps: ->
    catch: @::defaultCatch

  getInitialState: ->

        error: null

    promise.catch (error) =>
      @safelySetState promise,
        pending: false
        resolved: false
        rejected: true
        value: null
        error: error

  safelySetState: (promise, state) ->
    if @isMounted() and promise is @props.promise
      @setState state

  render: ->
    if @state.resolved
      try
        @renderResolved @state.value
      catch error
        @renderRejected error

    else if @state.rejected
      @renderRejected @state.error

    else
      # Until the initial promise is resolved or rejected, show the given children.
