Skip to content

Commit 0a5b1d8

Browse files
committed
Add Promise.all and Promise.race examples
1 parent cadb0ed commit 0a5b1d8

2 files changed

Lines changed: 38 additions & 0 deletions

File tree

JavaScript/8-all.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
'use strict';
2+
3+
const fetch = require('./6-fetch.js');
4+
5+
const baseUrl = 'http://localhost:3000/';
6+
7+
const promises = [
8+
fetch(baseUrl + '/person'),
9+
fetch(baseUrl + '/'),
10+
fetch(baseUrl + '/city')
11+
];
12+
13+
Promise.all(promises)
14+
.then(values => {
15+
console.log(values);
16+
})
17+
.catch(err => {
18+
console.log(err);
19+
});

JavaScript/9-race.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
'use strict';
2+
3+
const fetch = require('./6-fetch.js');
4+
5+
const baseUrl = 'http://localhost:3000/';
6+
7+
const promises = [
8+
fetch(baseUrl + '/person'),
9+
fetch(baseUrl + '/'),
10+
fetch(baseUrl + '/city')
11+
];
12+
13+
Promise.race(promises)
14+
.then(res => {
15+
console.log(res);
16+
})
17+
.catch(err => {
18+
console.log(err);
19+
});

0 commit comments

Comments
 (0)