节点异步脚本有时会意外结束

时间:2017-01-05 17:16:51

标签: node.js gulp ecmascript-6 es6-promise co

我有一个节点脚本,它调用许多进程来打包文件。大多数时候,这种方法效果很好,但偶尔会有一段时间(平均每5次来电中有1次?)它只是停在中间,总是在同一个地方。具体来说,日志失败时的结尾如下所示:

Finished task 1!
Compiling jsajax.js...
Compiling apps.js...

我没有收到任何错误或其他任何错误,所以我不确定该怎么看。这里的设置是我的主文件(index.js)使用co和generator调用所需的异步进程,并产生结果。其中一些是gulp,这是发生这个问题的地方。我在这里包含调用代码和gulp任务,因为代码的其余部分太长而无法显示所有内容。如果您认为需要,我很乐意加入更多内容。谢谢!

致电功能:

const createJS = function* createJS () {
  try {
    yield gulpFile.createJS();
    return 0;
  } catch(err) {
    console.error(err);
    return CONSTANTS.ERROR;
  }
};

Gulp任务:

const createJS = function () {
  const buildProps = PropertiesReader('build.properties'),
      distLoc = buildProps.get('distLoc'),
      installLoc = buildProps.get('installLoc'),
      updateLoc = buildProps.get('updateLoc'),
      installJSLoc = `${installLoc}/js`,
      updateJSLoc = `${updateLoc}/js`,
      jsajax = CONSTANTS.JSAJAX_FILES.map(file => distLoc + file),
      appsOnly = CONSTANTS.APPS_FILES.map(file => distLoc + file),
      apps = [...jsajax, ...appsOnly];
  let compile1, compile2;
  compile1 = new Promise((resolve, reject) => {
    console.log('Compiling jsajax.js...');
    gulp.src(jsajax)
      .pipe(sourcemaps.init())
      .pipe(concat('jsajax.js'))
      .pipe(uglify())
      .pipe(sourcemaps.write('./'))
      .pipe(gulp.dest(installJSLoc))
      .pipe(gulp.dest(updateJSLoc))
      .on('error', err => reject(err))
      .on('end', () => {
        console.log('jsajax.js compiled successfully!');
        resolve();
      });
  });
  compile2 = new Promise((resolve, reject) => {
    console.log('Compiling apps.js...');
    gulp.src(apps)
      .pipe(sourcemaps.init())
      .pipe(concat('apps.js'))
      .pipe(uglify())
      .pipe(sourcemaps.write('./'))
      .pipe(gulp.dest(installJSLoc))
      .pipe(gulp.dest(updateJSLoc))
      .on('error', err => reject(err))
      .on('end', () => {
        console.log('apps.js compiled successfully!');
        resolve();
      });
  });
  return Promise.all([compile1, compile2]);
};

0 个答案:

没有答案