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

Unified Diff: ui/webui/resources/js/cr.js

Issue 1758013002: WebUI: Allow rejecting a promise that is returned from cr.sendWithPromise(). (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: nit Created 4 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 side-by-side diff with in-line comments
Download patch
Index: ui/webui/resources/js/cr.js
diff --git a/ui/webui/resources/js/cr.js b/ui/webui/resources/js/cr.js
index 10ecb7d210bf69fcc52217cffeebb1242513e565..c78ab9df7e05a12b5faac72fadb8f8bc2f40edc2 100644
--- a/ui/webui/resources/js/cr.js
+++ b/ui/webui/resources/js/cr.js
@@ -318,9 +318,9 @@ var cr = function() {
/**
* The mapping used by the sendWithPromise mechanism to tie the Promise
* returned to callers with the corresponding WebUI response. The mapping is
- * from ID to the Promise resolver function; the ID is generated by
+ * from ID to the PromiseResolver helper; the ID is generated by
* sendWithPromise and is unique across all invocations of said method.
- * @type {!Object<!Function>}
+ * @type {!Object<!PromiseResolver>}
*/
var chromeSendResolverMap = {};
@@ -334,12 +334,17 @@ var cr = function() {
* supply any number of other arguments that will be included in the response.
* @param {string} id The unique ID identifying the Promise this response is
* tied to.
+ * @param {boolean} isSuccess Whether the request was successful.
* @param {*} response The response as sent from C++.
*/
- function webUIResponse(id, response) {
- var resolverFn = chromeSendResolverMap[id];
+ function webUIResponse(id, isSuccess, response) {
+ var resolver = chromeSendResolverMap[id];
delete chromeSendResolverMap[id];
- resolverFn(response);
+
+ if (isSuccess)
+ resolver.resolve(response);
+ else
+ resolver.reject(response);
}
/**
@@ -352,11 +357,11 @@ var cr = function() {
*/
function sendWithPromise(methodName, var_args) {
var args = Array.prototype.slice.call(arguments, 1);
- return new Promise(function(resolve, reject) {
- var id = methodName + '_' + createUid();
- chromeSendResolverMap[id] = resolve;
- chrome.send(methodName, [id].concat(args));
- });
+ var promiseResolver = new PromiseResolver();
+ var id = methodName + '_' + createUid();
+ chromeSendResolverMap[id] = promiseResolver;
+ chrome.send(methodName, [id].concat(args));
+ return promiseResolver.promise;
}
/**

Powered by Google App Engine
This is Rietveld 408576698