Answer the question
In order to leave comments, you need to log in
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}) }
/>
}
}
Answer the question
In order to leave comments, you need to log in
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();
}
})
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question