N
N
Nash2016-08-25 12:13:31
css
Nash, 2016-08-25 12:13:31

How to resolve a Promise based on data coming from a third party component?

Let's say there is a function that calls a promise and waits for its execution, but we need to ask the user how to split this very promise. Accordingly, we show a custom dialog (javscript confirm does not suit us, although it works) and from this very dialog we need to process this very promise.
promise

{
  closeModalView() { return this.closeView() }

  closeView() { 
    return new Promise( (resolve, reject) => {
      if (this.state.saveViewWithoutChanges) {
        return reject();
      }

      if (!this.state.saveViewWithoutChanges) {
        return resolve();
      }
    })
  }


  render() {
    <Dialog 
      onSuccessButtonClick={ () => this.setState({saveViewWithoutChanges: false}) }
      onCancelButtonClick={ () => this.setState({saveViewWithoutChanges: true}) }
    />
  }
}

Actually, we have the initialization of Promise before the call and, accordingly, mutating the state will not give us anything.
Question: how to resolve a Promise based on data received from a third-party component?
I can well assume that I'm dumb. Please correct if so.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
M
Maxim Lumen, 2016-06-05
@Maximum_Live

jqueryui.com/draggable but this does not help in any way?

V
Vitaly, 2016-08-25
@mrTyler

it is not clear when you call the same closeView() , if before the event, then it will work before the changes.
you do closeView().then(()=>{console.log('true');}).catch((error=>{console.log('error');}); and you will see that rather your promise dies just fine before the event
changes.promise is a one-time asynchronous task, and judging by your code, you think that the promise will wait for changes that you send to the
state.Clumsy option, in the success and kensel events, call the closeView( true) closeView(false)
and rewrite the function like this:

closeView(state) { 
    return new Promise( (resolve, reject) => {
      if (state) {
        return reject();
      }
else {
        return resolve();
      }
    })
  }

D
DenJel, 2016-08-25
@DenJel

And what prevents to initialize a promise after a mutation? Or store the promise itself in the constructor?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question