简体   繁体   中英

How to call async/await function asynchronously in other functions?

I create common function that will be called several different times and that function is async/await type.

const storeData = async () => {
   await User.create({name: "Test 123"})
}

Now if I want to call function storeData in many different functions asynchronously, do I call it with await in a front of it (option 1 bellow) or since it has async await inside of it, will be run as async function and there is no need for await in front (option 2 bellow)?

(Note: I could do workaround and make storeData to return a new Promise and then do the things inside the promise and be sure that await will really matters.)

Option 1.

async dummy1() {

   await storeData();

   ...rest of thte code

}

Option 2.

async dummy2() {

   storeData();

   ...rest of the code

}

In javascript, all asynchronous functions are identical to a regular function that returns a promise. The async is just syntactic sugar. So, if you use await before the function call, the function call will block execution, and if you don't use await then it'll run asynchronously, like you desire.

Read more here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM