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

Side by Side Diff: chrome/browser/resources/google_now/common_test_util.js

Issue 162273002: Convert Google Now's Authentication Manager to use Promises (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix Bug in Promises Created 6 years, 10 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 // Common test utilities. 5 // Common test utilities.
6 6
7 /** 7 /**
8 * Allows console.log output. 8 * Allows console.log output.
9 */ 9 */
10 var showConsoleLogOutput = false; 10 var showConsoleLogOutput = false;
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
75 /** 75 /**
76 * MockPromise 76 * MockPromise
77 * The JS test harness expects all calls to complete synchronously. 77 * The JS test harness expects all calls to complete synchronously.
78 * As a result, we can't use built-in JS promises since they run asynchronously. 78 * As a result, we can't use built-in JS promises since they run asynchronously.
79 * Instead of mocking all possible calls to promises, a skeleton 79 * Instead of mocking all possible calls to promises, a skeleton
80 * implementation is provided to get the tests to pass. 80 * implementation is provided to get the tests to pass.
81 */ 81 */
82 var Promise = function() { 82 var Promise = function() {
83 function PromisePrototypeObject(asyncTask) { 83 function PromisePrototypeObject(asyncTask) {
84 var result; 84 var result;
85 var resolved = false;
85 asyncTask( 86 asyncTask(
86 function(asyncResult) { 87 function(asyncResult) {
87 result = asyncResult; 88 result = asyncResult;
89 resolved = true;
88 }, 90 },
89 function() {}); // Errors are unsupported. 91 function(asyncFailureResult) {
92 result = asyncResult;
93 resolved = false;
94 });
90 95
91 function then(callback) { 96 function then(callback) {
92 callback.call(null, result); 97 if (resolved) {
98 callback.call(null, result);
99 }
100 return this;
93 } 101 }
94 return {then: then, isPromise: true}; 102
103 // Promises use the function name catch to call back error handlers.
skare_ 2014/02/13 23:53:52 (no action required, just a suggestion based on fi
robliao 2014/02/14 00:05:11 Done.
104 // We can't use catch since function or variable names cannot use the word
skare_ 2014/02/13 23:53:52 or just "We can't use 'catch' since it's reserved"
robliao 2014/02/14 00:05:11 This isn't strictly true, since it works fine for
105 // catch.
106 function catchFunc(callback) {
107 if (!resolved) {
108 callback.call(null, result);
109 }
110 return this;
111 }
112
113 var promiseObj = {then: then, isPromise: true};
114 // Using the name 'catch' above during initialization will trigger an error.
115 promiseObj.catch = catchFunc;
skare_ 2014/02/13 23:37:03 does promiseObj.catch = function(...) {} work?
robliao 2014/02/13 23:43:20 It does, but the named functions follows other non
skare_ 2014/02/13 23:53:52 return {then: then, isPromise: true, catch: catchF
robliao 2014/02/14 00:05:11 This won't work since "catch" is reserved in objec
skare_ 2014/02/14 00:19:26 will catch* you in person since I might have const
robliao 2014/02/14 01:57:04 And done. On 2014/02/14 00:19:26, Travis Skare wro
116 return promiseObj;
95 } 117 }
96 118
97 function all(arrayOfPromises) { 119 function all(arrayOfPromises) {
98 var results = []; 120 var results = [];
99 for (i = 0; i < arrayOfPromises.length; i++) { 121 for (i = 0; i < arrayOfPromises.length; i++) {
100 if (arrayOfPromises[i].isPromise) { 122 if (arrayOfPromises[i].isPromise) {
101 arrayOfPromises[i].then(function(result) { 123 arrayOfPromises[i].then(function(result) {
102 results[i] = result; 124 results[i] = result;
103 }); 125 });
104 } else { 126 } else {
105 results[i] = arrayOfPromises[i]; 127 results[i] = arrayOfPromises[i];
106 } 128 }
107 } 129 }
108 var promise = new PromisePrototypeObject(function(resolve) { 130 var promise = new PromisePrototypeObject(function(resolve) {
109 resolve(results); 131 resolve(results);
110 }); 132 });
111 return promise; 133 return promise;
112 } 134 }
113 PromisePrototypeObject.all = all; 135 PromisePrototypeObject.all = all;
114 return PromisePrototypeObject; 136 return PromisePrototypeObject;
115 }(); 137 }();
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698