https://github.com/danielrohers/node-async-await
Example node async-await
https://github.com/danielrohers/node-async-await
Last synced: 10 months ago
JSON representation
Example node async-await
- Host: GitHub
- URL: https://github.com/danielrohers/node-async-await
- Owner: danielrohers
- Created: 2017-04-12T08:26:20.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2017-04-12T21:26:13.000Z (about 9 years ago)
- Last Synced: 2025-06-03T02:10:42.823Z (about 1 year ago)
- Language: JavaScript
- Size: 1000 Bytes
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# node-async-await
Example node async-await
## Old (only with promises)
[index-old.js](index-old.js)
```js
const getText = () => {
return new Promise((resolve, reject) => {
let timeout = setTimeout(() => {
resolve('Hi :)');
clearTimeout(timeout);
}, 3000);
})
}
const makeText = () => {
console.log('init');
getText()
.then(console.log)
.then(() => console.log('end'));
}
makeText();
```
[series-old.js](series-old.js)
```js
const getText = () => {
return new Promise((resolve, reject) => {
let timeout = setTimeout(() => {
console.log('Hi :)');
resolve();
clearTimeout(timeout);
}, 1000);
})
}
const makeText = () => {
console.log('init');
getText()
.then(getText)
.then(getText)
.then(getText)
.then(getText)
.then(() => console.log('end'))
}
makeText();
```
## New (with async-await)
[index.js](index.js)
```js
const getText = () => {
return new Promise((resolve, reject) => {
let timeout = setTimeout(() => {
resolve('Hi :)');
clearTimeout(timeout);
}, 3000);
})
}
const makeText = async () => {
console.log('init');
console.log(await getText());
console.log('end');
}
makeText();
```
[series.js](series.js)
```js
const getText = () => {
return new Promise((resolve, reject) => {
let timeout = setTimeout(() => {
console.log('Hi :)');
resolve();
clearTimeout(timeout);
}, 1000);
})
}
const makeText = async () => {
console.log('init');
await getText();
await getText();
await getText();
await getText();
await getText();
console.log('end');
}
makeText();
```