等待代码执行输入

时间:2018-06-16 06:07:19

标签: javascript node.js promise dialog electron

在Electron应用程序中,我需要更改一个对象数组,我从Excel文件中接收。

只要此函数中没有用户输入,这样就可以了,但是用户可以选择将带有文件名的目录链接到该数组中。

如果我打开一个对话框来选择目录,我需要我的脚本停止并等待用户输入,但看不到如何做到这一点。简化我的代码如下:

let initialArray = [];

let Arr1=[];
let Arr2=[];
let arrayFiles=[];

let outputData = [];

document.getElementById('importExcel').addEventListener('change', handleFile, false);

function handleFile(e) {
    //get initial Array an fill it with Data
    let q = new Promise(function (resolve, reject) {
        for (let x of initialArray) {
            //extract Data for Arr1 and push it there
            //extract Data for Arr2 and push it there
        }
        resolve();
    }
    q.then(workOnArr1).then(getFiles).then(workOnArr1and2).then(mergeData).catch(console.log.bind(console));
}
function workOnArr1() {
    //rearranging and adding Data to Arr1
}
function getFiles() {
    if (document.getElementById("getFileData").checked) {
        dialog.showOpenDialog({properties: ['openDirectory']}, function (filePaths) {
            //here it should stop until a directory is chosen and the filenames
            // are read into arrayFiles
            fs.readdirSync(filePaths[0]).forEach(file => {
                arrayFiles.push(file)
            });
        });
    }
}
function workOnArr1and2() {
    //alter Arr1 and integrate the Data of arrayFiles into it
    for (let x of Arr1) {
        //that does not work, because the script does not stop at the dialog,
        //arrayFiles is always empty when this line is executed
        for (let y of arrayFiles) 
            if (somecondition) {
                x.subArray = y
            }
    }
    //alter and reshape Arr2
}
function mergeData() {
    outputData = _.concat(Arr1,Arr2);
    //write outputData to DB
} 

据我所知,then在执行下一个函数之前等待一个函数完成后,用Promise链接函数。但是,因为我似乎错过了一些关键点,或者我的假设可能完全错误。

1 个答案:

答案 0 :(得分:1)

你如何处理重要的异步代码到Promise链 - 例如你的getFiles函数......需要一个Promise当#34;

完成对话框时,返回该结果
function getFiles() {
    if (document.getElementById("getFileData").checked) {
        return new Promise(resolve => {
            dialog.showOpenDialog({properties: ['openDirectory']}, function (filePaths) { 
            //here it should stop until a directory is chosen and the filenames
            // are read into arrayFiles
                fs.readdirSync(filePaths[0]).forEach(file => {
                    arrayFiles.push(file)
                });
                // or better still
                //
                // arrayFiles.push(...fs.readdirSync(filePaths[0]))
                //
                resolve();
            });
        });
    }
}

你做的一件事看起来非常规,有一堆全局对象在一个函数中更新并在下一个阅读中,Promises的美丽不一定有这样的全局污染

e.g。一个简化的例子

Promise.resolve(5)
.then(x => x * 2)
.then(x => x - 1)
.then(x => x / 3)

而不是

let x = 5;
Promise.resolve()
.then(() => x = x * 2)
.then(() => x = x - 1)
.then(() => x = x / 3)

相同的结果,但不需要变量x

话虽如此,你可能正在做的最好的方法是做到这一点,那里的代码不足以知道发生了什么,问题中// description of some vague functionality过多public void reader(){ try{ FileInputStream fis = new FileInputStream("C:\\file.dat"); ObjectInputStream ois = new ObjectInputStream(fis); int j = model.getRowCount(); for(int i = 0; i<j;i++){ Object a = model.getValueAt(i,1); Object b = ois.read((byte[]) a); if(a.equals(b)){ doner(); fis.close(); } else{ JOptionPane.showMessageDialog(null,"You do not have "+a.toString()+" in your inventory. Add it to your inventory first.","Item Not Found", JOptionPane.INFORMATION_MESSAGE); } } }catch (IOException e){ JOptionPane.showMessageDialog(null,"File file.dat not found in C:\\. Make a empty file in the folder and save it with file.dat","File not found", JOptionPane.ERROR_MESSAGE); } } : p