promisify
This commit is contained in:
parent
d912bce1c5
commit
364e707b2a
9 changed files with 120 additions and 2 deletions
|
@ -0,0 +1,20 @@
|
|||
|
||||
That's the case when knowing how it works inside is helpful.
|
||||
|
||||
Just treat `async` call as promise and attach `.then` to it:
|
||||
```js run
|
||||
async function wait() {
|
||||
await new Promise(resolve => setTimeout(resolve, 1000));
|
||||
|
||||
return 10;
|
||||
}
|
||||
|
||||
function f() {
|
||||
// shows 10 after 1 second
|
||||
*!*
|
||||
wait().then(result => alert(result));
|
||||
*/!*
|
||||
}
|
||||
|
||||
f();
|
||||
```
|
20
1-js/11-async/08-async-await/03-async-from-regular/task.md
Normal file
20
1-js/11-async/08-async-await/03-async-from-regular/task.md
Normal file
|
@ -0,0 +1,20 @@
|
|||
|
||||
# Call async from non-async
|
||||
|
||||
We have a "regular" function. How to call `async` from it and use its result?
|
||||
|
||||
```js
|
||||
async function wait() {
|
||||
await new Promise(resolve => setTimeout(resolve, 1000));
|
||||
|
||||
return 10;
|
||||
}
|
||||
|
||||
function f() {
|
||||
// ...what to write here?
|
||||
// we need to call async wait() and wait to get 10
|
||||
// remember, we can't use "await"
|
||||
}
|
||||
```
|
||||
|
||||
P.S. The task is technically very simple, but the question is quite common for developers new to async/await.
|
Loading…
Add table
Add a link
Reference in a new issue