Electron 对话框对象不是 return 任何东西,Promise { <pending> }

Electron dialog object not return anything, says Promise { <pending> }

我正在尝试对我正在构建的教程应用程序实施 I/O 操作。其他一切正常,我遇到的唯一问题是应用程序没有 console.log() 预期的结果,这应该是对话框中所选文件的位置。

这是截图

正如 r3wt 所说,当您看到 promise<pending> 时,这意味着您没有正确等待异步函数的 return。尝试将 getFileFromUser 函数更改为异步函数,如下所示:

const getFileFromUser = async () => {

    // Triggers the OS' Open File Dialog box. We also pass it as a Javascript
    // object of different configuration arguments to the function

    //This operation is asynchronous and needs to be awaited
    const files = await dialog.showOpenDialog(mainWindow, {
        // The Configuration object sets different properties on the Open File Dialog 
        properties: ['openFile']
    });

    // If we don't have any files, return early from the function
    if (!files) {
        return;
    }

    // Pulls the first file out of the array

    //const file = files[0];
    // Reads from the file and converts the resulting buffer to a string
    //const content = fs.readFileSync(file).toString();

    // Log the Files to the Console
    console.log(files)
}