##// END OF EJS Templates
Clean up promises code some more.
Jason Grout -
Show More
@@ -70,23 +70,21 b' define(['
70 70
71 71 this.comms[comm_id] = utils.load_class(content.target_name, content.target_module,
72 72 this.targets).then(function(target) {
73
74 var comm = new Comm(content.target_name, comm_id);
75 comm.kernel = that.kernel;
76 try {
77 var response = target(comm, msg);
78 if (response instanceof Promise) {
79 return response.then(function() { return Promise.resolve(comm); });
73 var comm = new Comm(content.target_name, comm_id);
74 comm.kernel = that.kernel;
75 try {
76 var response = target(comm, msg);
77 } catch (e) {
78 comm.close();
79 that.unregister_comm(comm);
80 var wrapped_error = new utils.WrappedError("Exception opening new comm", e);
81 console.error(wrapped_error);
82 return Promise.reject(wrapped_error);
80 83 }
81 } catch (e) {
82 comm.close();
83 that.unregister_comm(comm);
84 var wrapped_error = new utils.WrappedError("Exception opening new comm", e);
85 console.error(wrapped_error);
86 return Promise.reject(wrapped_error);
87 }
88 return Promise.resolve(comm);
89 }, utils.reject('Could not open comm', true));
84 // Regardless of the target return value, we need to
85 // then return the comm
86 return Promise.resolve(response).then(function() {return comm;});
87 }, utils.reject('Could not open comm', true));
90 88 return this.comms[comm_id];
91 89 };
92 90
@@ -104,6 +102,8 b' define(['
104 102 } catch (e) {
105 103 console.log("Exception closing comm: ", e, e.stack, msg);
106 104 }
105 // don't return a comm, so that further .then() functions
106 // get an undefined comm input
107 107 });
108 108 };
109 109
@@ -120,7 +120,7 b' define(['
120 120 } catch (e) {
121 121 console.log("Exception handling comm msg: ", e, e.stack, msg);
122 122 }
123 return Promise.resolve(comm);
123 return comm;
124 124 });
125 125 };
126 126
@@ -49,24 +49,24 b' define(['
49 49 WidgetManager.prototype.display_view = function(msg, model) {
50 50 // Displays a view for a particular model.
51 51 var that = this;
52 return new Promise(function(resolve, reject) {
53 var cell = that.get_msg_cell(msg.parent_header.msg_id);
54 if (cell === null) {
55 reject(new Error("Could not determine where the display" +
56 " message was from. Widget will not be displayed"));
57 } else if (cell.widget_subarea) {
58 var dummy = $('<div />');
59 cell.widget_subarea.append(dummy);
60 that.create_view(model, {cell: cell}).then(function(view) {
52 var cell = this.get_msg_cell(msg.parent_header.msg_id);
53 if (cell === null) {
54 return Promise.reject(new Error("Could not determine where the display" +
55 " message was from. Widget will not be displayed"));
56 } else if (cell.widget_subarea) {
57 var dummy = $('<div />');
58 cell.widget_subarea.append(dummy);
59 return this.create_view(model, {cell: cell}).then(
60 function(view) {
61 61 that._handle_display_view(view);
62 62 dummy.replaceWith(view.$el);
63 63 view.trigger('displayed');
64 resolve(view);
65 }, function(error) {
66 reject(new utils.WrappedError('Could not display view', error));
64 return view;
65 },
66 function(error) {
67 return Promise.reject(new utils.WrappedError('Could not display view', error));
67 68 });
68 }
69 });
69 }
70 70 };
71 71
72 72 WidgetManager.prototype._handle_display_view = function (view) {
@@ -95,7 +95,6 b' define(["widgets/js/manager",'
95 95 } finally {
96 96 that.state_lock = null;
97 97 }
98 return Promise.resolve();
99 98 }, utils.reject("Couldn't set model state", true));
100 99 },
101 100
General Comments 0
You need to be logged in to leave comments. Login now