Skip to content

Commit

Permalink
Remove Timer promises catch clause (ampproject#5758)
Browse files Browse the repository at this point in the history
* Remove Timer promises catch clause

Timer's promises can only fail because of being unable to schedule.

* Temp fix amp-form tests

Re: ampproject#5775
  • Loading branch information
jridgewell authored and Vanessa Pasque committed Dec 22, 2016
1 parent 6d31c63 commit ce9c721
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 25 deletions.
8 changes: 4 additions & 4 deletions extensions/amp-form/0.1/test/test-amp-form.js
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ describe('amp-form', () => {
expect(form.className).to.not.contain('amp-form-submit-error');
expect(form.className).to.not.contain('amp-form-submit-success');
fetchJsonResolver();
return timer.promise(0).then(() => {
return timer.promise(5).then(() => {
expect(ampForm.state_).to.equal('submit-success');
expect(form.className).to.not.contain('amp-form-submitting');
expect(form.className).to.not.contain('amp-form-submit-error');
Expand Down Expand Up @@ -398,7 +398,7 @@ describe('amp-form', () => {
expect(form.className).to.not.contain('amp-form-submit-error');
expect(form.className).to.not.contain('amp-form-submit-success');
fetchJsonRejecter();
return timer.promise(0).then(() => {
return timer.promise(5).then(() => {
expect(button1.hasAttribute('disabled')).to.be.false;
expect(button2.hasAttribute('disabled')).to.be.false;
expect(ampForm.state_).to.equal('submit-error');
Expand Down Expand Up @@ -442,7 +442,7 @@ describe('amp-form', () => {
};
ampForm.handleSubmit_(event);
fetchJsonRejecter({responseJson: {message: 'hello there'}});
return timer.promise(0).then(() => {
return timer.promise(5).then(() => {
expect(ampForm.templates_.findAndRenderTemplate.called).to.be.true;
expect(ampForm.templates_.findAndRenderTemplate.calledWith(
errorContainer, {message: 'hello there'})).to.be.true;
Expand Down Expand Up @@ -489,7 +489,7 @@ describe('amp-form', () => {
};
ampForm.handleSubmit_(event);
fetchJsonResolver({'message': 'What What'});
return timer.promise(0).then(() => {
return timer.promise(5).then(() => {
expect(ampForm.templates_.findAndRenderTemplate.called).to.be.true;
expect(ampForm.templates_.findAndRenderTemplate.calledWith(
successContainer, {'message': 'What What'})).to.be.true;
Expand Down
28 changes: 7 additions & 21 deletions src/service/timer-impl.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,21 +99,14 @@ export class Timer {
* @template RESULT
*/
promise(opt_delay, opt_result) {
let timerKey = null;
return new Promise((resolve, reject) => {
timerKey = this.delay(() => {
timerKey = -1;
return new Promise(resolve => {
const timerKey = this.delay(() => {
resolve(opt_result);
}, opt_delay);

if (timerKey == -1) {
reject(new Error('Failed to schedule timer.'));
}
}).catch(error => {
// Clear the timer. The most likely reason is "cancel" signal.
if (timerKey != -1) {
this.cancel(timerKey);
throw new Error('Failed to schedule timer.');
}
throw error;
});
}

Expand All @@ -129,21 +122,14 @@ export class Timer {
* @template RESULT
*/
timeoutPromise(delay, opt_racePromise, opt_message) {
let timerKey = null;
const delayPromise = new Promise((_resolve, reject) => {
timerKey = this.delay(() => {
timerKey = -1;
const timerKey = this.delay(() => {
reject(user().createError(opt_message || 'timeout'));
}, delay);

if (timerKey == -1) {
reject(new Error('Failed to schedule timer.'));
}
}).catch(error => {
// Clear the timer. The most likely reason is "cancel" signal.
if (timerKey != -1) {
this.cancel(timerKey);
throw new Error('Failed to schedule timer.');
}
throw error;
});
if (!opt_racePromise) {
return delayPromise;
Expand Down

0 comments on commit ce9c721

Please sign in to comment.