本文介绍了电子上的异步问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发Electron.js应用程序,但遇到了异步问题.我在三个要依次执行的if语句中有三个函数(downdloadFile()),但它们异步运行;我已经尝试过使用async/await,但是没有用.这是我的代码:

I'm currently working on an Electron.js app and I'm stuck on an asynchrony problem. I have three function (downdloadFile()) inside three if statements to be executed in sequence but they run asynchronously; I already tried using async/await but it didn't worked.Here's my code:

ipcMain.on('play',(event,payload) => {
        launcherConfig.savedRam = payload.savedRam;
        launcherConfig.savedMaxPermSize = payload.savedMaxPermSize;
        if(payload.selectedPacket) {
            //FIRST IF
            if (!fs.existsSync(launcherDir + "\\natives")) {
                downloadFile("http://soulnetwork.it/launcher/natives.zip", launcherDir + "\\natives.zip", true, 'natives');
            }
            //SECOND IF
            if (!fs.existsSync(launcherDir + "\\bin")) {
                downloadFile("http://soulnetwork.it/launcher/bin.zip", launcherDir + "\\bin.zip", true, 'bin');
            }
            //THIRD IF
            if (launcherConfig.installed_modpacks.includes(payload.selectedPacket)) {
                launchMinecraft(payload.selectedPacket);
                saveConfig();
            } else {
                downloadFile(`http://soulnetwork.it/launcher/modpacks/${payload.selectedPacket}.zip`, launcherDir + "\\modpacks\\" + payload.selectedPacket + '.zip', true, payload.selectedPacket, launchMinecraft);
                launcherConfig.installed_modpacks.push(payload.selectedPacket);
                saveConfig();
            }
        }
}

function downloadFile(file_url , targetPath, showProcess, packetName, callback){
// Save variable to know progress
var received_bytes = 0;
var total_bytes = 0;

var req = request({
    method: 'GET',
    uri: file_url
});

var progressWindow = null;

if(showProcess){
    progressWindow = new BrowserWindow({width: 300, height: 60, title: `Downloading ${packetName}`,icon: '../public/images/sn.png'})
    progressWindow.setProgressBar(0.0);
    progressWindow.loadURL('file://' + __dirname + '/views/download.ejs');
    progressWindow.setMenu(null);
}

var out = fs.createWriteStream(targetPath);
req.pipe(out);

req.on('response', function ( data ) {
    // Change the total bytes value to get progress later.
    total_bytes = parseInt(data.headers['content-length' ]);
});

req.on('data', function(chunk) {
    // Update the received bytes
    received_bytes += chunk.length;
    if(showProcess)
        showProgress(progressWindow,received_bytes, total_bytes);
});

req.on('end', function() {
    if(showProcess)
        progressWindow.close();
    if(targetPath.includes('.zip')){
        var zip = new archiver(targetPath);
        zip.extractAllTo(targetPath.substr(0,targetPath.length-4-packetName.length));
        fs.unlinkSync(targetPath);
    }
    if(callback)
        callback(packetName);
    console.log("RETURN");
    return;
});
}

使用回调系统会造成很多麻烦,而且这也是多余的,我希望能找到另一种解决方案.谢谢您的时间!

Using the callback system creates so much caos and it is also redundant, I was hoping for another solution.Thank you for your time!

推荐答案

您当前在console.log("RETURN")的位置可以解析由downloadFile返回的Promise.然后,您可以简单地等待if分支中的调用(然后将async回调传递给ipcMain当然).

Where you currently console.log("RETURN") you can resolve a Promise which is returned by downloadFile. Then you can simply await the calls in your if branches (and pass async callback to ipcMain of course).

以更简单的形式显示的结构如下所示

The structure in a more simple form would look like the followings

function doJob () {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      console.log('RETURN')
      resolve()
    }, 2000)
  })
}
// ipcMain.on('play', async (event, payload) => { ... })
(async () => {
  if (true) {
    await doJob()
  }
  if (true) {
    await doJob()
  }
})()

这篇关于电子上的异步问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 12:37