最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Using AsyncAwait in ES6 React .JS - Stack Overflow

programmeradmin3浏览0评论

I have a NetCore2 app with ReactJS installed.

My React app does something like this ->

  • User inputs into a field
  • axios makes a call to an api to get the user and/or list of users
  • The input field shows suggestions based on what was saved in PeopleList state.

I'm using Office Fabric UI React - People Picker (in case it matters).

My problem here is that i have no idea how i could use async/await in my code.

If i put something like this

async asynconFilterChanged = (filterText, currentPersonas, limitResults) => {

VS Code throws an error telling me that async is only for .ts files. Also if i use await in any other functions i will get an error such as await is a reserved word.

As far as i know async/await is not TS only...(or am i wrong?).

I'm using the older Babel with the ENV preset.

Let me know if i need to provide more code, but TLDR what i want is to only start the filter function after my axios is done pushing items into the PeopleList state (array of objects).

Thanks!

I have a NetCore2 app with ReactJS installed.

My React app does something like this ->

  • User inputs into a field
  • axios makes a call to an api to get the user and/or list of users
  • The input field shows suggestions based on what was saved in PeopleList state.

I'm using Office Fabric UI React - People Picker (in case it matters).

My problem here is that i have no idea how i could use async/await in my code.

If i put something like this

async asynconFilterChanged = (filterText, currentPersonas, limitResults) => {

VS Code throws an error telling me that async is only for .ts files. Also if i use await in any other functions i will get an error such as await is a reserved word.

As far as i know async/await is not TS only...(or am i wrong?).

I'm using the older Babel with the ENV preset.

Let me know if i need to provide more code, but TLDR what i want is to only start the filter function after my axios is done pushing items into the PeopleList state (array of objects).

Thanks!

Share Improve this question asked Nov 21, 2018 at 13:35 Dante R.Dante R. 9325 gold badges13 silver badges42 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 15

When using an arrow function you should write async/await like this:

const asynconFilteredChanged = async () => {
 // code here 
}

Check out Syntax for async arrow function

As per syntax try to follow above codebase,

asynconFilterChanged = async (filterText, currentPersonas, limitResults) => {
            try {
                await yourFucntion = (limitResults) => {
                ....Your logic...
                }
            } catch(error) {
                console.error(error);
            }

Yes, try to use and touch async-await guide

发布评论

评论列表(0)

  1. 暂无评论