forked from and-digital/and-workshop-corejs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path13_promises.test.js
148 lines (121 loc) · 3.33 KB
/
13_promises.test.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
describe('About promises', () => {
/*
Task 1) Refactor the anAsyncFunction to use shorthand object promise notation
*/
const anAsyncFunction = result => {
return new Promise((resolve, reject) => {
if (result) {
return resolve([0, 1, 2]);
} else {
return reject(' Oh no :( ');
}
});
};
it('should, when resolved, call the then', done => {
anAsyncFunction(true).then(result => {
expect(result).toEqual([0, 1, 2]);
done();
});
});
it('should, when rejected, call the catch handler', done => {
anAsyncFunction(false).catch(e => {
expect(e).toBeTruthy();
done();
});
});
/*
Task 2) Abstract out the .then calls into a separate function, not anonymous
Think about:
- What happens if you call .then without returning anything?
- Does a .then have to return a promise? Can it? Try wrapping addOne's function with: Promise.resolve()
*/
const addOne = result => result + 1;
it('should add up to 6!', done => {
return Promise.resolve(0)
.then(num => num + 2)
.then(num => num + 2)
.then(num => num + 2)
.then(function(result) {
expect(result).toBe(6);
})
.then(done);
});
/*
Task 3) Refactor to be flat like the one before
*/
it('should add to 10 without using a big chain of promises', done => {
Promise.resolve(0)
.then(addOne)
.then(value => {
return Promise.resolve(value)
.then(value => {
return Promise.resolve(value)
.then(addOne)
.then(addOne)
.then(addOne);
})
.then(addOne)
.then(addOne);
})
.then(addOne)
.then(value => {
return Promise.resolve(value)
.then(addOne)
.then(addOne)
.then(addOne);
})
.then(result => {
expect(result).toBe(10);
})
.then(done);
});
/*
Task 4) Rewrite to use Promise.all
Think about:
- Do the two API calls need to be made separately?
- What could be the impact on performance?
*/
const getUser = result => {
return Promise.resolve({
name: 'Alice'
});
};
const getPlans = result => {
return Promise.resolve({
id: 4,
plan: 'My plan'
});
};
const subscribe = (userName, id) => {
return `Create the plan called ${userName} for plan ID ${id}`;
};
it('should get Alices plan', done => {
return getUser()
.then(user => {
return getPlans().then(plans => {
return subscribe(user.name, plans.id);
});
})
.then(string => {
expect(string).toBe('Create the plan called Alice for plan ID 4');
done();
});
});
/*
Task 5) Promise.all returns an array, but that means we have to access with the gross array[0] syntax.
Rewrite the .then to use the spread operator instead
*/
const hey = (firstName, lastName) => {
return `Hey ${firstName} ${lastName}!`;
};
it('should greet Bob using the spread operator', done => {
return Promise.all([Promise.resolve('Bob'), Promise.resolve('Miggins')])
.then(data => {
return hey(data[0], data[1]);
})
.then(string => {
expect(string).toBe('Hey Bob Miggins!');
done();
});
});
});