OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 /** |
| 6 * @param {string} error |
| 7 * @return {!Promise.<T>} |
| 8 * @template T |
| 9 */ |
| 10 Promise.rejectWithError = function(error) |
| 11 { |
| 12 return Promise.reject(new Error(error)); |
| 13 } |
| 14 |
| 15 /** |
| 16 * @param {function((T|undefined))} callback |
| 17 * @return {!Promise.<T>} |
| 18 * @template T |
| 19 */ |
| 20 Promise.prototype.thenOrCatch = function(callback) |
| 21 { |
| 22 return this.then(callback, reject.bind(this)); |
| 23 |
| 24 /** |
| 25 * @param {*} e |
| 26 * @this {Promise} |
| 27 */ |
| 28 function reject(e) |
| 29 { |
| 30 this._reportError(e); |
| 31 callback(undefined); |
| 32 } |
| 33 } |
| 34 |
| 35 Promise.prototype.done = function() |
| 36 { |
| 37 this.catchAndReport(); |
| 38 } |
| 39 |
| 40 /** |
| 41 * @return {!Promise} |
| 42 */ |
| 43 Promise.prototype.catchAndReport = function() |
| 44 { |
| 45 return this.catch(this._reportError.bind(this)); |
| 46 } |
| 47 |
| 48 /** |
| 49 * @param {*} e |
| 50 */ |
| 51 Promise.prototype._reportError = function(e) |
| 52 { |
| 53 if (e instanceof Error) |
| 54 console.error(e.stack); |
| 55 else |
| 56 console.error(e); |
| 57 } |
OLD | NEW |