【问题标题】:How would I go about waiting for a child process to finish before telling Node.js to continue executing code?在告诉 Node.js 继续执行代码之前,我将如何等待子进程完成?
【发布时间】:2018-07-29 13:04:22
【问题描述】:

所以我正在构建一个 Electron 和 React 应用程序。我正在使用 ghost-script 创建某些 pdf 文件的 imgs,我想知道如何告诉 Node.js 在打开窗口并更改应用程序中的状态之前等待创建 imgs。这些 img 被用作组件的 src,当组件尝试加载 img 时,它在某种程度上保留了一个损坏的状态,因为当状态更新时 img src 不存在。

// this is where I set the state before sending all the data to the renderer process(the front end of the App)
function getStateReady(theState, event) {
  let pdfFiles = scanDirectory(currentDir);
  let pdfNames = getPdfName(pdfFiles);
  imageUrls = getImgName(pdfFiles);
  console.log(imageUrls);
  createImg(pdfFiles, pdfNames);
  switch (theState) {
    case 'initialState':
        mainWindow.webContents.on('did-finish-load', () => {
          mainWindow.webContents.send('initialState', pdfFiles, imageUrls);
        }) 
        break;
    case 'secondState-reply':
        event.sender.send('secondState-reply', pdfFiles, imageUrls);
        break;
    default:
          console.log('a param was missing');
  }
}


//these to functions take a pdf file path and its name to create an img
function createImg(pdfPaths, pdfNames) {
  pdfNames.forEach((item, index) => {
    if(fs.existsSync(path.join(rootDirectory, 'src', 'imgs', item.replace('.pdf', '.jpg')))) {
      console.log('image exists');
    } 
    else {
      console.log("creating image");
      child(returnProcess(pdfPaths[index], item), (err, stdout) => {
        if(err) {
          console.log(err)
        }
        console.log(stdout)
      }) 
    }
  })
}

function returnProcess(pdfPath, pdfName) {
  let newPdf = `"${pdfPath}"`
  let output = `"${path.join(rootDirectory, 'src', 'imgs', pdfName.replace('.pdf', '.jpg'))}"`;
  let mainProcess = `"C:\\Program Files\\gs\\gs9.23\\bin\\gswin64c.exe" -q -o ${output} -sDEVICE=pngalpha -dLastPage=1 ${newPdf}`

  return mainProcess;
}

【问题讨论】:

  • 将检查它。谢谢
  • 我使用过异步和等待,但我不确定我是否正确使用它。即使执行了所有功能,问题仍然存在,因为仍在创建图像。也许问题出在幽灵脚本本身。

标签: javascript node.js reactjs electron child-process


【解决方案1】:

我对代码和逻辑并不完全确定,而且我从未使用过 ghost-script,但我可以在此建议解决此问题的是使用回调函数。基本上将回调函数传递给createImg 方法并执行回调函数,该函数将告诉NodeJS 进程已完成,前端应该能够显示创建的图像。

以下是更新代码。

// this is where I set the state before sending all the data to the renderer process(the front end of the App)
function getStateReady(theState, event) {
  let pdfFiles = scanDirectory(currentDir);
  let pdfNames = getPdfName(pdfFiles);
  imageUrls = getImgName(pdfFiles);
  console.log(imageUrls);
  createImg(pdfFiles, pdfNames, function (){
  switch (theState) {
    case 'initialState':
        mainWindow.webContents.on('did-finish-load', () => {
          mainWindow.webContents.send('initialState', pdfFiles, imageUrls);
        }) 
        break;
    case 'secondState-reply':
        event.sender.send('secondState-reply', pdfFiles, imageUrls);
        break;
    default:
          console.log('a param was missing');
  }
 });
}


//these to functions take a pdf file path and its name to create an img
function createImg(pdfPaths, pdfNames, fun) {
  pdfNames.forEach((item, index) => {
    if(fs.existsSync(path.join(rootDirectory, 'src', 'imgs', item.replace('.pdf', '.jpg')))) {
      console.log('image exists');
    } 
    else {
      console.log("creating image");
      child(returnProcess(pdfPaths[index], item), (err, stdout) => {
        if(err) {
          console.log(err)
        }
        console.log(stdout)
      }) 
    }
  })
  // call the callback function
  fun.call();
}

function returnProcess(pdfPath, pdfName) {
  let newPdf = `"${pdfPath}"`
  let output = `"${path.join(rootDirectory, 'src', 'imgs', pdfName.replace('.pdf', '.jpg'))}"`;
  let mainProcess = `"C:\\Program Files\\gs\\gs9.23\\bin\\gswin64c.exe" -q -o ${output} -sDEVICE=pngalpha -dLastPage=1 ${newPdf}`

  return mainProcess;
}

【讨论】:

    猜你喜欢
    • 2011-11-01
    • 2021-12-29
    • 1970-01-01
    • 2018-11-28
    • 1970-01-01
    • 1970-01-01
    • 2010-09-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多