Skip to content

Commit c3ab2e6

Browse files
committed
Resolved DrkSephy#38
1 parent dcfdf13 commit c3ab2e6

File tree

1 file changed

+30
-19
lines changed

1 file changed

+30
-19
lines changed

README.md

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -936,34 +936,45 @@ been resolved/rejected is immutable - it will never change.
936936
Here is a practical example of using Promises:
937937

938938
```javascript
939-
var fetchJSON = function(url) {
940-
return new Promise((resolve, reject) => {
941-
$.getJSON(url)
942-
.done((json) => resolve(json))
943-
.fail((xhr, status, err) => reject(status + err.message));
944-
});
945-
};
939+
var request = require('request');
940+
941+
return new Promise((resolve, reject) => {
942+
request.get(url, (error, response, body) => {
943+
if (body) {
944+
resolve(JSON.parse(body));
945+
} else {
946+
resolve({});
947+
}
948+
});
949+
});
946950
```
947951

948952
We can also **parallelize** Promises to handle an array of asynchronous
949953
operations by using `Promise.all()`:
950954

951955
```javascript
952-
var urls = [
953-
'http://www.api.com/items/1234',
954-
'http://www.api.com/items/4567'
956+
let urls = [
957+
'/api/commits',
958+
'/api/issues/opened',
959+
'/api/issues/assigned',
960+
'/api/issues/completed',
961+
'/api/issues/comments',
962+
'/api/pullrequests'
955963
];
956964

957-
var urlPromises = urls.map(fetchJSON);
965+
let promises = urls.map((url) => {
966+
return new Promise((resolve, reject) => {
967+
$.ajax({ url: url })
968+
.done((data) => {
969+
resolve(data);
970+
});
971+
});
972+
});
958973

959-
Promise.all(urlPromises)
960-
.then(function (results) {
961-
results.forEach(function (data) {
962-
});
963-
})
964-
.catch(function (err) {
965-
console.log('Failed: ', err);
966-
});
974+
Promise.all(promises)
975+
.then((results) => {
976+
// Do something with results of all our promises
977+
});
967978
```
968979

969980
<sup>[(back to table of contents)](#table-of-contents)</sup>

0 commit comments

Comments
 (0)