admin管理员组文章数量:1356830
I want to use async/wait
with React ponentDidMount()
method but I am getting await is a reserved word error. I also tried wrap the statement in Immediate Invoked Function but it didn't help.
async ponentDidMount() {
this.geoLocation.getAddress().then(location => {
if (location.address != null && location.error != "undefined") {
let fifteenMins = [];
await this.getFifteenMinsData(y, x).then(
data => {
fifteenMins = data["forecasts"];
}
);
console.log(fifteenMins);
}
});
}
If I remove the await
keyword, then I get null
in console.log, but if I do console log right before fifteenMins = data["forecasts"];
then I get data.
Related question: Await is a reserved word error inside async function
I want to use async/wait
with React ponentDidMount()
method but I am getting await is a reserved word error. I also tried wrap the statement in Immediate Invoked Function but it didn't help.
async ponentDidMount() {
this.geoLocation.getAddress().then(location => {
if (location.address != null && location.error != "undefined") {
let fifteenMins = [];
await this.getFifteenMinsData(y, x).then(
data => {
fifteenMins = data["forecasts"];
}
);
console.log(fifteenMins);
}
});
}
If I remove the await
keyword, then I get null
in console.log, but if I do console log right before fifteenMins = data["forecasts"];
then I get data.
Related question: Await is a reserved word error inside async function
Share Improve this question asked Aug 15, 2018 at 16:06 Maihan NijatMaihan Nijat 9,36513 gold badges68 silver badges117 bronze badges 2- what's your node version? – Dhaval Jardosh Commented Aug 15, 2018 at 16:07
- valentinog./blog/how-async-await-in-react – Dhaval Jardosh Commented Aug 15, 2018 at 16:08
2 Answers
Reset to default 8async
functions always return promises. Since ponentDidMount
isn't designed/documented as an async
function, React doesn't do anything with the promise it returns. If you use an async
function for this, be sure to wrap all its code in try
/catch
so that all errors are caught and you don't end up with an unhandled exception (which bees an unhandled rejection).
The problem is that you're trying to use await
in a non-async
function: The callback you've passed then
. When using async
/await
, you almost never use then
. Instead:
async ponentDidMount() {
try {
const location = await this.geoLocation.getAddress();
if (location.address != null && location.error != "undefined") {
const data = await this.getFifteenMinsData(y, x);
let fifteenMins = data["forecasts"];
console.log(fifteenMins);
}
} catch (err) {
// Do something with the fact an error occurred
}
}
Or avoiding returning a promise from ponentDidMount
by using an IIFE:
ponentDidMount() {
(async () => {
const location = await this.geoLocation.getAddress();
if (location.address != null && location.error != "undefined") {
const data = await this.getFifteenMinsData(y, x);
let fifteenMins = data["forecasts"];
console.log(fifteenMins);
}
})()
.catch(error => {
// Do something with the fact an error occurred
});
}
Or don't use an async
function at all (but async
functions are really handy):
ponentDidMount() {
this.geoLocation.getAddress()
.then(location => {
if (location.address != null && location.error != "undefined") {
return this.getFifteenMinsData(y, x)
.then(data => {
let fifteenMins = data["forecasts"];
console.log(fifteenMins);
});
}
})
.catch(error => {
// Do something with the fact an error occurred
});
}
Side note: This pair of lines:
const data = await this.getFifteenMinsData(y, x);
let fifteenMins = data["forecasts"];
can be written like this if you like, destructuring the result into the fifteenMins
variable:
let {fifteenMins: forecasts} = await this.getFifteenMinsData(y, x);
Similarly, if you did decide to go with the non-async
version, you can do that in the parameter list of the then
handler:
.then(({fifteenMins: forecasts}) => {
console.log(fifteenMins);
});
if you are using await you dont have to use then
let data= await this.getFifteenMinsData(y, x);
edit
let location = await this.geoLocation.getAddress();
//do your stuff
if (location.address != null && location.error != "undefined") {
let fifteenMins = [];
let data = await this.getFifteenMinsData(y, x);
fifteenMins = data["forecasts"];
console.log(fifteenMins);
}
本文标签: javascriptHow to use async await with React componentDidMount() methodStack Overflow
版权声明:本文标题:javascript - How to use async await with React componentDidMount() method? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743984409a2571215.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论