File tree Expand file tree Collapse file tree 4 files changed +40
-0
lines changed
1-js/11-async/07-async-await Expand file tree Collapse file tree 4 files changed +40
-0
lines changed File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
1
+
2
+ That's the case when knowing how it works inside is helpful.
3
+
4
+ Just treat ` async ` call as promise and attach ` .then ` to it:
5
+ ``` js run
6
+ async function wait () {
7
+ await new Promise (resolve => setTimeout (resolve, 1000 ));
8
+
9
+ return 10 ;
10
+ }
11
+
12
+ function f () {
13
+ // shows 10 after 1 second
14
+ * ! *
15
+ wait ().then (result => alert (result));
16
+ */ ! *
17
+ }
18
+
19
+ f ();
20
+ ```
Original file line number Diff line number Diff line change
1
+
2
+ # Call async from non-async
3
+
4
+ We have a "regular" function. How to call ` async ` from it and use its result?
5
+
6
+ ``` js
7
+ async function wait () {
8
+ await new Promise (resolve => setTimeout (resolve, 1000 ));
9
+
10
+ return 10 ;
11
+ }
12
+
13
+ function f () {
14
+ // ...what to write here?
15
+ // we need to call async wait() and wait to get 10
16
+ // remember, we can't use "await"
17
+ }
18
+ ```
19
+
20
+ P.S. The task is technically very simple, but the question is quite common for developers new to async/await.
You can’t perform that action at this time.
0 commit comments