async await example stackoverflow
Example 1: async await javascript stack overflow
function makePromise(x) {
return new Promise(resolve => {
setTimeout(() => {
resolve(x);
}, 1000);
});
}
async function asyncFunc() {
var x = await makePromise(1);
console.log(x);
return x;
}
const returnedProm = asyncFunc();
returnedProm.then((x) => console.log(x));
Example 2: async await react stackoverflow
constructor() {
this.state = { returnData: null }
}
async componentDidMount() {
const returnData = await this.Post(...);
this.setState({ returnData });
}
async Post(body) {
try{
const options = {
method: 'POST',
uri: 'XXXXXXXXXXXXXXXXXXXX',
body: body
}
return rp(options);
}catch(e){console.warn(e)}
}
render() {
const { returnData } = this.state;
}