Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/403.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用承诺或回调延迟脚本操作_Javascript_Callback - Fatal编程技术网

Javascript 如何使用承诺或回调延迟脚本操作

Javascript 如何使用承诺或回调延迟脚本操作,javascript,callback,Javascript,Callback,我编写了一个节点脚本,它生成一组文件并自动填充它们。然而,我在输出中注意到,成功消息在调用消息之后出现了很多次 除了在已有代码中使用承诺或回调之外,我对它们不是很熟悉。我将如何着手编写以下内容以一步一步的方式运行 var fileGen = function(componentName) { // Each createFile instance should run only after the previous one has completed. createFile('v

我编写了一个节点脚本,它生成一组文件并自动填充它们。然而,我在输出中注意到,成功消息在调用消息之后出现了很多次

除了在已有代码中使用承诺或回调之外,我对它们不是很熟悉。我将如何着手编写以下内容以一步一步的方式运行

var fileGen = function(componentName) {
    // Each createFile instance should run only after the previous one has completed.
    createFile('view.php', componentName)
    createFile('style.styl', componentName)
    createFile('script.js', componentName)
    createFile('style.print.styl', componentName)
    // finishUp should only run when all createFile instances have completed.
    finishUp()
}

var createFile = function (fileName, componentName, doc) {
  // Tell the user what is happening
  console.log(chalk.blue('\nCreating', fileName, '...'))
  // Bring in the view.php scaffold file
  var scaffold = './scaffold/' + fileName
  fs.readFile(scaffold, 'utf8', function (err, data) {
    if (err) return console.log(chalk.red(err))
    var result = data.replace(/%cname%/g, componentName)
    if (doc) {
      var d = new Date()
      result = result.replace(/%cfname%/g, doc.componentName)
      result = result.replace(/%cdesc%/g, doc.componentDesc)
      result = result.replace(/%cauthor%/g, doc.userName)
      result = result.replace(/%cagithub%/g, doc.userGithub)
      result = result.replace(/%creationdate%/g, d.getDate() + '/' + d.getMonth() + '/' + d.getFullYear())
    }

    fs.writeFile('./src/components/' + componentName + '/' + fileName, result, function (err) {
      if (err) return console.log(chalk.red(err))
      console.log(chalk.green(fileName, 'created!'))
    })
  })
}
我觉得这是我应该能够做的事情,可能以前也做过,但由于某种原因,我今天无法集中精力解决它。

承诺:


啊,我听说过蓝鸟,但不知道它是什么。谢谢,我来看看。我在已有代码中使用它们是什么意思?你可能想看看,非常感谢!这是我第一次看到承诺对我来说完全有意义。好吧,请不要这样做。在最低级别上承诺,即分别为readFile和writeFile。还请注意,reject不是返回,其余代码仍在执行!使用elseAlso Promise.all似乎比chaining@Bergi不能保证。所有的createFile函数都是异步执行的,例如,不按照Alex Ward想要的顺序执行?@Bergi,如果有人没有使用Bluebird或提供Promisify的类似库,这个答案仍然可以接受吗?
var fileGen = function(componentName) {
    // Each createFile instance should run only after the previous one has completed.
    createFile('view.php', componentName)
    .then(() => {
        createFile('style.styl', componentName);
    })
    .then(() => {
        createFile('script.js', componentName)
    })
    .then(() => {
        createFile('style.print.styl', componentName)
    })
    .then(() => {
        // finishUp should only run when all createFile instances have completed.
        finishUp()    
    });
}

var createFile = function (fileName, componentName, doc) {
  // Tell the user what is happening
  console.log(chalk.blue('\nCreating', fileName, '...'))
  // Bring in the view.php scaffold file
  var scaffold = './scaffold/' + fileName

  return new Promise((resolve, reject) => {
    fs.readFile(scaffold, 'utf8', function (err, data) {
      if (err) {
        console.log(chalk.red(err))
        reject(err)
        return
      }
      var result = data.replace(/%cname%/g, componentName)
      if (doc) {
        var d = new Date()
        result = result.replace(/%cfname%/g, doc.componentName)
        result = result.replace(/%cdesc%/g, doc.componentDesc)
        result = result.replace(/%cauthor%/g, doc.userName)
        result = result.replace(/%cagithub%/g, doc.userGithub)
        result = result.replace(/%creationdate%/g, d.getDate() + '/' + d.getMonth() + '/' + d.getFullYear())
      }

      fs.writeFile('./src/components/' + componentName + '/' + fileName, result, function (err) {
        if (err) {
            console.log(chalk.red(err))
            reject(err)
            return
        } else {
          console.log(chalk.green(fileName, 'created!'))
          resolve(); // promise is now fulfilled, the next chained `then` will be called   }
      })
    })
  });
}