Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(430)

Unified Diff: Source/devtools/front_end/platform/Promise.js

Issue 673163004: [DevTools] Extract platform module. (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Fix inspector-protocol tests Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: Source/devtools/front_end/platform/Promise.js
diff --git a/Source/devtools/front_end/platform/Promise.js b/Source/devtools/front_end/platform/Promise.js
new file mode 100644
index 0000000000000000000000000000000000000000..f854b567a09cf95dd520136a6c8f2a8c19324f01
--- /dev/null
+++ b/Source/devtools/front_end/platform/Promise.js
@@ -0,0 +1,68 @@
+// Copyright 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+/**
+ * @param {string} error
+ * @return {!Promise.<T>}
+ * @template T
+ */
+Promise.rejectWithError = function(error)
+{
+ return Promise.reject(new Error(error));
+}
+
+/**
+ * @param {function((T|undefined))} callback
+ * @return {!Promise.<T>}
+ * @template T
+ */
+Promise.prototype.thenOrCatch = function(callback)
+{
+ return this.then(callback, reject.bind(this));
+
+ /**
+ * @param {*} e
+ * @this {Promise}
+ */
+ function reject(e)
+ {
+ this._reportError(e);
+ callback(undefined);
+ }
+}
+
+Promise.prototype.done = function()
+{
+ this.catchAndReport();
+}
+
+/**
+ * @return {!Promise}
+ */
+Promise.prototype.catchAndReport = function()
+{
+ return this.catch(this._reportError.bind(this));
+}
+
+/**
+ * @param {*} e
+ */
+Promise.prototype._reportError = function(e)
+{
+ if (e instanceof Error)
+ console.error(e.stack);
+ else
+ console.error(e);
+}
+
+// FIXME: This performance optimization should be moved to blink so that all developers could enjoy it.
+// console is retrieved with V8Window.getAttribute method which is slow. Here we copy it to a js variable for faster access.
+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.
+console.__originalAssert = console.assert;
+console.assert = function(value, message)
+{
+ if (value)
+ return;
+ console.__originalAssert(value, message);
+}

Powered by Google App Engine
This is Rietveld 408576698