-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpromises.js
46 lines (38 loc) · 1.67 KB
/
promises.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// Promises
// Execute something and wait for the response.
// For example: Call rest api and wait for response.
// We use Axios for external calls. Axios implements promises. For now I create a mocked promise below that will return data when completed.
const externalResponse = new Promise((resolve, reject) => {
setTimeout(() => {
// Everything went ok, we use resolve to conclude the promise and return the values.
resolve({status: '200', data: 'External data call', somethingElse: 'example'});
}, 2000);
});
// Execute the promise and do something with the data we received.
externalResponse.then((response) => {
response.somethingElse = 'example updated in a promise chain';
// Pass the object to the next promise chain.
return response;
}).then((updatedResponse) => {
// Output the result to console.
console.log(updatedResponse);
});
// Example of a promise that fails
const externalResponseFail = new Promise((resolve, reject) => {
setTimeout(() => {
// Something really really bad happened, in stead of resolve we use reject now. This will be catched in the .catch below.
reject({status: '500', data: 'External data call', somethingElse: 'example'});
}, 2000);
});
// Execute the promise and do something with the data we received.
externalResponseFail.then((response) => {
response.somethingElse = 'example updated in a promise chain';
// Pass the object to the next promise chain.
return response;
}).then((updatedResponse) => {
// We will not get here
console.log(updatedResponse);
}).catch((e) => {
// We will get here when something in the promise chain failed.
console.log('Error occurred', e);
});