Chrome The message port closed before a response was received

3 min read

Add a return true. It will force chrome to wait for the response.
添加一个返回true。它将迫使chrome等待响应。

chrome.extension.onMessage.addListener((request, sender, sendResponse) => {
  // Look up a term from the dictionary using the Ajax API.
  const lookupURL = `http://dictionary-lookup.org/${request.arg}`;
  sendRequest(lookupURL)
    .then(resp => {
      sendResponse(resp || '{}');
    })
    .catch(error => {
      sendResponse('{}');
    });
  return true; // Inform Chrome that we will make a delayed sendResponse
});