Skip to content

Commit 82ac341

Browse files
Merge #1196
1196: Use async/await to simplify Promise chains r=locks
2 parents a587d01 + de0d24d commit 82ac341

File tree

12 files changed

+174
-176
lines changed

12 files changed

+174
-176
lines changed

app/components/api-token-row.js

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,32 +13,33 @@ export default Component.extend({
1313
},
1414

1515
actions: {
16-
saveToken() {
17-
this.get('api_token')
18-
.save()
19-
.then(() => this.set('serverError', null))
20-
.catch(err => {
21-
let msg;
22-
if (err.errors && err.errors[0] && err.errors[0].detail) {
23-
msg = `An error occurred while saving this token, ${err.errors[0].detail}`;
24-
} else {
25-
msg = 'An unknown error occurred while saving this token';
26-
}
27-
this.set('serverError', msg);
28-
});
16+
async saveToken() {
17+
try {
18+
await this.get('api_token').save();
19+
this.set('serverError', null);
20+
} catch(err) {
21+
let msg;
22+
if (err.errors && err.errors[0] && err.errors[0].detail) {
23+
msg = `An error occurred while saving this token, ${err.errors[0].detail}`;
24+
} else {
25+
msg = 'An unknown error occurred while saving this token';
26+
}
27+
this.set('serverError', msg);
28+
}
2929
},
30-
revokeToken() {
31-
this.get('api_token')
32-
.destroyRecord()
33-
.catch(err => {
34-
let msg;
35-
if (err.errors && err.errors[0] && err.errors[0].detail) {
36-
msg = `An error occurred while revoking this token, ${err.errors[0].detail}`;
37-
} else {
38-
msg = 'An unknown error occurred while revoking this token';
39-
}
40-
this.set('serverError', msg);
41-
});
30+
31+
async revokeToken() {
32+
try {
33+
await this.get('api_token').destroyRecord();
34+
} catch(err) {
35+
let msg;
36+
if (err.errors && err.errors[0] && err.errors[0].detail) {
37+
msg = `An error occurred while revoking this token, ${err.errors[0].detail}`;
38+
} else {
39+
msg = 'An unknown error occurred while revoking this token';
40+
}
41+
this.set('serverError', msg);
42+
}
4243
},
4344
}
4445
});

app/components/email-input.js

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -92,22 +92,21 @@ export default Component.extend({
9292
this.set('value', this.get('prevEmail'));
9393
},
9494

95-
resendEmail() {
95+
async resendEmail() {
9696
let user = this.get('user');
9797

98-
this.get('ajax').raw(`/api/v1/users/${user.id}/resend`, {
99-
method: 'PUT'
100-
})
101-
.then(() => this.set('disableResend', true))
102-
.catch((error) => {
103-
if (error.payload) {
104-
this.set('isError', true);
105-
this.set('emailError', `Error in resending message: ${error.payload.errors[0].detail}`);
106-
} else {
107-
this.set('isError', true);
108-
this.set('emailError', 'Unknown error in resending message');
109-
}
110-
});
98+
try {
99+
await this.get('ajax').raw(`/api/v1/users/${user.id}/resend`, { method: 'PUT' });
100+
this.set('disableResend', true);
101+
} catch(error) {
102+
if (error.payload) {
103+
this.set('isError', true);
104+
this.set('emailError', `Error in resending message: ${error.payload.errors[0].detail}`);
105+
} else {
106+
this.set('isError', true);
107+
this.set('emailError', 'Unknown error in resending message');
108+
}
109+
}
111110
}
112111
}
113112
});

app/components/pending-owner-invite-row.js

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7,39 +7,38 @@ export default Component.extend({
77
inviteError: 'default error message',
88

99
actions: {
10-
acceptInvitation(invite) {
10+
async acceptInvitation(invite) {
1111
invite.set('accepted', true);
12-
invite.save()
13-
.then(() => {
14-
this.set('isAccepted', true);
15-
})
16-
.catch((error) => {
17-
this.set('isError', true);
18-
if (error.payload) {
19-
this.set('inviteError',
20-
`Error in accepting invite: ${error.payload.errors[0].detail}`
21-
);
22-
} else {
23-
this.set('inviteError', 'Error in accepting invite');
24-
}
25-
});
12+
13+
try {
14+
await invite.save();
15+
this.set('isAccepted', true);
16+
17+
} catch(error) {
18+
this.set('isError', true);
19+
if (error.payload) {
20+
this.set('inviteError', `Error in accepting invite: ${error.payload.errors[0].detail}`);
21+
} else {
22+
this.set('inviteError', 'Error in accepting invite');
23+
}
24+
}
2625
},
27-
declineInvitation(invite) {
26+
27+
async declineInvitation(invite) {
2828
invite.set('accepted', false);
29-
invite.save()
30-
.then(() => {
31-
this.set('isDeclined', true);
32-
})
33-
.catch((error) => {
34-
this.set('isError', true);
35-
if (error.payload) {
36-
this.set('inviteError',
37-
`Error in declining invite: ${error.payload.errors[0].detail}`
38-
);
39-
} else {
40-
this.set('inviteError', 'Error in declining invite');
41-
}
42-
});
29+
30+
try {
31+
await invite.save();
32+
this.set('isDeclined', true);
33+
34+
} catch(error) {
35+
this.set('isError', true);
36+
if (error.payload) {
37+
this.set('inviteError', `Error in declining invite: ${error.payload.errors[0].detail}`);
38+
} else {
39+
this.set('inviteError', 'Error in declining invite');
40+
}
41+
}
4342
}
4443
}
4544
});

app/controllers/crate/owners.js

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export default Controller.extend({
88
username: '',
99

1010
actions: {
11-
addOwner() {
11+
async addOwner() {
1212
this.set('error', false);
1313
this.set('invited', false);
1414

@@ -19,35 +19,35 @@ export default Controller.extend({
1919
return false;
2020
}
2121

22-
return this.get('crate').inviteOwner(username).then(() => {
22+
try {
23+
await this.get('crate').inviteOwner(username);
2324
this.set('invited', `An invite has been sent to ${username}`);
24-
}).catch((error) => {
25+
26+
} catch(error) {
2527
if (error.payload) {
26-
this.set('error',
27-
`Error sending invite: ${error.payload.errors[0].detail}`
28-
);
28+
this.set('error', `Error sending invite: ${error.payload.errors[0].detail}`);
2929
} else {
3030
this.set('error', 'Error sending invite');
3131
}
32-
});
32+
}
3333
},
3434

35-
removeOwner(user) {
35+
async removeOwner(user) {
3636
this.set('removed', false);
3737

38-
return this.get('crate').removeOwner(user.get('login')).then(() => {
38+
try {
39+
await this.get('crate').removeOwner(user.get('login'));
3940
this.set('removed', `User ${user.get('login')} removed as crate owner`);
4041

4142
this.get('crate.owner_user').removeObject(user);
42-
}).catch((error) => {
43+
44+
} catch(error) {
4345
if (error.payload) {
44-
this.set('removed',
45-
`Error removing owner: ${error.payload.errors[0].detail}`
46-
);
46+
this.set('removed', `Error removing owner: ${error.payload.errors[0].detail}`);
4747
} else {
4848
this.set('removed', 'Error removing owner');
4949
}
50-
});
50+
}
5151
}
5252
}
5353
});

app/controllers/dashboard.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,19 +42,21 @@ export default Controller.extend({
4242
}),
4343

4444
actions: {
45-
loadMore() {
45+
async loadMore() {
4646
this.set('loadingMore', true);
4747
let page = (this.get('myFeed').length / 10) + 1;
4848

49-
this.get('ajax').request(`/api/v1/me/updates?page=${page}`).then((data) => {
50-
let versions = data.versions.map(version =>
51-
this.store.push(this.store.normalize('version', version)));
49+
try {
50+
let data = await this.get('ajax').request(`/api/v1/me/updates?page=${page}`);
51+
52+
let versions = data.versions.map(version => this.store.push(this.store.normalize('version', version)));
5253

5354
this.get('myFeed').pushObjects(versions);
5455
this.set('hasMore', data.meta.more);
55-
}).finally(() => {
56+
57+
} finally {
5658
this.set('loadingMore', false);
57-
});
59+
}
5860
}
5961
}
6062
});

app/routes/application.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,17 @@ export default Route.extend({
88

99
flashMessages: service(),
1010

11-
beforeModel() {
12-
if (this.session.get('isLoggedIn') &&
13-
this.session.get('currentUser') === null) {
14-
this.get('ajax').request('/api/v1/me').then((response) => {
11+
async beforeModel() {
12+
if (this.session.get('isLoggedIn') && this.session.get('currentUser') === null) {
13+
try {
14+
let response = await this.get('ajax').request('/api/v1/me');
1515
this.session.set('currentUser', this.store.push(this.store.normalize('user', response.user)));
16-
}).catch(() => this.session.logoutUser()).finally(() => {
16+
} catch(_) {
17+
this.session.logoutUser();
18+
} finally {
1719
window.currentUserDetected = true;
1820
$(window).trigger('currentUserDetected');
19-
});
21+
}
2022
} else {
2123
window.currentUserDetected = true;
2224
}

app/routes/confirm.js

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,34 @@ export default Route.extend({
55
flashMessages: service(),
66
ajax: service(),
77

8-
model(params) {
9-
return this.get('ajax').raw(`/api/v1/confirm/${params.email_token}`, { method: 'PUT', data: {} })
10-
.then(() => {
11-
/* We need this block to reload the user model from the database,
12-
without which if we haven't submitted another GET /me after
13-
clicking the link and before checking their account info page,
14-
the user will still see that their email has not yet been
15-
validated and could potentially be confused, resend the email,
16-
and set up a situation where their email has been verified but
17-
they have an unverified token sitting in the DB.
8+
async model(params) {
9+
try {
10+
await this.get('ajax').raw(`/api/v1/confirm/${params.email_token}`, { method: 'PUT', data: {} });
1811

19-
Suggestions of a more ideomatic way to fix/test this are welcome!
20-
*/
21-
if (this.session.get('isLoggedIn')) {
22-
this.get('ajax').request('/api/v1/me').then((response) => {
23-
this.session.set('currentUser', this.store.push(this.store.normalize('user', response.user)));
24-
});
25-
}
26-
})
27-
.catch((error) => {
28-
if (error.payload) {
29-
this.get('flashMessages').queue(`Error in email confirmation: ${error.payload.errors[0].detail}`);
30-
return this.replaceWith('index');
31-
} else {
32-
this.get('flashMessages').queue(`Unknown error in email confirmation`);
33-
return this.replaceWith('index');
34-
}
35-
});
12+
/* We need this block to reload the user model from the database,
13+
without which if we haven't submitted another GET /me after
14+
clicking the link and before checking their account info page,
15+
the user will still see that their email has not yet been
16+
validated and could potentially be confused, resend the email,
17+
and set up a situation where their email has been verified but
18+
they have an unverified token sitting in the DB.
19+
20+
Suggestions of a more ideomatic way to fix/test this are welcome!
21+
*/
22+
if (this.session.get('isLoggedIn')) {
23+
this.get('ajax').request('/api/v1/me').then((response) => {
24+
this.session.set('currentUser', this.store.push(this.store.normalize('user', response.user)));
25+
});
26+
}
27+
28+
} catch(error) {
29+
if (error.payload) {
30+
this.get('flashMessages').queue(`Error in email confirmation: ${error.payload.errors[0].detail}`);
31+
return this.replaceWith('index');
32+
} else {
33+
this.get('flashMessages').queue(`Unknown error in email confirmation`);
34+
return this.replaceWith('index');
35+
}
36+
}
3637
}
3738
});

0 commit comments

Comments
 (0)