代码之家  ›  专栏  ›  技术社区  ›  Torf

等待代码执行以进行输入

  •  0
  • Torf  · 技术社区  · 7 年前

    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 回复  |  直到 7 年前
        1
  •  1
  •   Jaromanda X    7 年前

    它是 重要的 getFiles

    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();
                });
            });
        }
    }
    

    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