Skip to content

Commit 3113f7d

Browse files
committed
up
1 parent 06d7123 commit 3113f7d

File tree

4 files changed

+40
-0
lines changed

4 files changed

+40
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
```
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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.

0 commit comments

Comments
 (0)