promise .then() 在 Electron.js 中不起作用

问题描述 投票:0回答:1

我在渲染器中有一个功能,允许用户保存 json 数据。对根系统的访问发生在 Electron 的 main.js 文件中。我希望仅在完成后才显示有关成功保存 json 数据的消息。为此,我使用了

promise
.then()
方法,理论上应该可以正常工作,但我很难理解出了什么问题。

//renderer.js
function concatAndSave() {
  let dataArray = jsonArrLeft.concat(jsonArrRight);
  const promise = new Promise((resolve, reject) => {
    window.electron.saveJson(dataArray);
    resolve("JSON data saved successfully!");
  });
  promise.then((info) => {
    closeSaveConfigWindow();
    const currentTime = new Date();
    textArea.value +=
      info +
      "[" +
      currentTime.getHours() +
      ":" +
      currentTime.getMinutes() +
      ":" +
      currentTime.getSeconds() +
      "]" +
      "\n\n";
    textArea.scrollTop = textArea.scrollHeight;
    removeStars(tableLeft.querySelector("#mf-table-body"), unsavedLeft);
    removeStars(tableRight.querySelector("#mf-table-right"), unsavedRight);
  });
}

// main.js

ipcMain.on("saveJson", (event, data) => {
    saveJSONToFile(data);
});

function saveJSONToFile(data) {
  const options = {
    title: "Save JSON File",
    defaultPath: "data.json",
    filters: [{ name: "JSON Files", extensions: ["json"] }],
  };

  dialog
    .showSaveDialog(win, options)
    .then((result) => {
      if (!result.canceled) {
        const filePath = result.filePath;
        const jsonData = JSON.stringify(data);

        // Write the JSON data to the selected file
        fs.writeFile(filePath, jsonData, "utf-8", (err) => {
          if (err) {
            console.error(err);
          } else {
            console.log("JSON data saved successfully.");
          }
        });
      }
    })
    .catch((err) => {
      console.error(err);
    });
}

javascript node.js async-await electron es6-promise
1个回答
0
投票

正如评论中提到的(然后我也意识到),

main.js
没有等待,即它只被触发一次,仅此而已。

所以我稍微更改了代码,并在

contextBrige
中的
preload.js
中添加了新频道:

contextBridge.exposeInMainWorld("electron", {
  jsonSuccess: (data) => {
    ipcRenderer.on("jsonSuccess", data);
  },
})

main.js
调用,从
function saveJSONToFile(data)
:

// FYI: look the original function in the question
fs.writeFile(filePath, jsonData, "utf-8", (err) => {
  if (err) {
    console.error(err);
  } else {
    win.webContents.send("jsonSuccess", true);
    console.log("JSON data saved successfully.");
   }
});
function concatAndSave() {
  let dataArray = jsonArrLeft.concat(jsonArrRight);
  window.electron.saveJson(dataArray); // let it do the rest in main.js
}

window.electron.jsonSuccess((e, data) => {
// triggers only from main.js in case of successful saving
  if (data) {
    closeSaveConfigWindow();
    const currentTime = new Date();
    textArea.value +=
      "JSON data saved successfully" +
      "[" +
      currentTime.getHours() +
      ":" +
      currentTime.getMinutes() +
      ":" +
      currentTime.getSeconds() +
      "]" +
      "\n\n";
    textArea.scrollTop = textArea.scrollHeight;
    removeStars(tableLeft.querySelector("#mf-table-body"), unsavedLeft);
    removeStars(tableRight.querySelector("#mf-table-right"), unsavedRight);
  }
});

© www.soinside.com 2019 - 2024. All rights reserved.