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 } | |
58 | |
59 // FIXME: This performance optimization should be moved to blink so that all dev elopers could enjoy it. | |
60 // console is retrieved with V8Window.getAttribute method which is slow. Here we copy it to a js variable for faster access. | |
61 console = console; | |
apavlov
2014/10/31 13:26:26
This is not quite related to promises
dgozman
2014/10/31 13:56:06
Moved to utilities.
| |
62 console.__originalAssert = console.assert; | |
63 console.assert = function(value, message) | |
64 { | |
65 if (value) | |
66 return; | |
67 console.__originalAssert(value, message); | |
68 } | |
OLD | NEW |