promise in react js code example
Example 1: react promises
componentWillMount() {
var promise = new Promise( (resolve, reject) => {
let name = 'Paul'
if (name === 'Paul') {
resolve("Promise resolved successfully");
}
else {
reject(Error("Promise rejected"));
}
});
let obj = {newName: ''};
promise.then( result => {
this.setState({name: result});
}, function(error) {
this.setState({name: error});
});
}
Example 2: what is promises in reactjs
componentWillMount() {
var promise = new Promise( (resolve, reject) => {
let name = 'Paul'
if (name === 'Paul') {
resolve("Promise resolved successfully");
}
else {
reject(Error("Promise rejected"));
}
});
let obj = {newName: ''};
promise.then( result => {
this.setState({name: result});
}, function(error) {
this.setState({name: error});
});
}