Why doesn't async/await in the example?

There is such code:


You must copy the select from the DOM and before it processes the library Choices.js (further it is inserted by clicking in the sandbox it did not display). Wanted to solve the problem with async and await:
async function f1() {
 copyDiv await() // copy div
 await choicesInit('select', {searchEnabled: false, shouldSort: false, itemSelectText: "}) // handled by the library
 await c1() // print copied div
}
f1();
But it's not working. Function choicesInit processes select before the function copyDiv copy the select statement into a variable (this can be understood from the fact that the select is hidden and labeled as classes). I can't understand why.
April 7th 20 at 10:52
1 answer
April 7th 20 at 10:54
Solution
choicesInit copyDiv and c1 you have a regular synchronous functions. They do not need to write the await

Now I will look further and will unsubscribe if I find other schools

slightly changed your example without await-s and inserting the test lead into the console to track the order of calls.
https://jsfiddle.net/c02zeh3n/

the output in the correct order: 0, 1, 2, 3, 4, 5 (see the output in the console) and then await you to work out right
I tried on kollegah to do the same where copyDiv() I was returning choicesInit(), the result is the same - Else commented on April 7th 20 at 10:57
@Else, I'm a little confused what exactly you want to achieve.
Why do you think that code does not wait for the completion of choicesInit. Savil was in response to your (slightly modernized) is an example. The output in the console clearly shows that the order of calls was observed - helga_Cartwright14 commented on April 7th 20 at 11:00
@Else, And it says that the problem is something else. How can I understand that the problem remained (or been eliminated)? I delien to see (or not see) in the alert-huh? - helga_Cartwright14 commented on April 7th 20 at 11:03
@helga_Cartwright14,
I delien to see (or not see) in the alert-huh?
To see that the variable is pure select (DOM-e it will have already been processed by the library), without classes, which hangs in the library. I need it :)

I do need to do the following:
1) Copy the select from the DOM and
2) Edit library select points on the page
3) Then, click user button you copied earlier select is inserted into the DOM and again processed by the library.

But the whole story did not drag in the sandbox, so as not to confuse - Else commented on April 7th 20 at 11:06
please note that choicesInit you also synchronous. accordingly await on it too.
Don't know STO returns the constructor of the class Choices, but if it returns a promise, you should wait for him to run inside choicesInit. This will require choicesInit to make async and return from it promis - helga_Cartwright14 commented on April 7th 20 at 11:09
@helga_Cartwright14, damn it. Here what result I need:

https://jsfiddle.net/w9mp82uc/3/ - Else commented on April 7th 20 at 11:12
@Else, sorry waste. Glad you got it. I hope at least something was useful for you. - helga_Cartwright14 commented on April 7th 20 at 11:15

Find more questions by tags JavaScript