| 1094 | // to translate the message into a resolved promise or a rejected |
| 1095 | // promise). |
| 1096 | const sendPromisedResult = promise => { |
| 1097 | promise.then(msg => { |
| 1098 | // send the message value. |
| 1099 | sendResponse(msg); |
| 1100 | }, error => { |
| 1101 | // Send a JSON representation of the error if the rejected value |
| 1102 | // is an instance of error, or the object itself otherwise. |
| 1103 | let message; |
| 1104 | if (error && (error instanceof Error || typeof error.message === "string")) { |
| 1105 | message = error.message; |
| 1106 | } else { |
| 1107 | message = "An unexpected error occurred"; |
| 1108 | } |
| 1109 | sendResponse({ |
| 1110 | __mozWebExtensionPolyfillReject__: true, |
| 1111 | message |
| 1112 | }); |
| 1113 | }).catch(err => { |
| 1114 | // Print an error on the console if unable to send the response. |
| 1115 | console.error("Failed to send onMessage rejected reply", err); |
| 1116 | }); |
| 1117 | }; |
| 1118 | |
| 1119 | // If the listener returned a Promise, send the resolved value as a |
| 1120 | // result, otherwise wait the promise related to the wrappedSendResponse |