简体   繁体   English

如何在CoffeeScript中进行回调

[英]How can I make a callback in CoffeeScript

I have this little bit of code mixed in with node.js and it allows me to create an unordered list of the directory. 我将这段代码与node.js混合在一起,它使我可以创建目录的无序列表。 What I want is for the parent looping function to pause and wait for the child loop to finish and then continue with the code. 我想要的是让父循环功能暂停并等待子循环完成,然后继续执行代码。

This is my code: 这是我的代码:

listFolders = (dir, callback) ->
  readDir = "#{__dirname}/public/#{dir}"
  fs.readdir readDir, (err, files) ->
  files.forEach (file) ->
    dirname = file
    path = "#{dir}/#{file}"
    readPath = "#{readDir}/#{file}"

    fs.stat readPath, (err, stat) ->
      if stat && stat.isDirectory()
        console.log "<li class='folder' data-path='#{dir}' data-name='#{dir}/#{dirname}'><span>#{dirname}</span><ul>"
        listFolders path, () ->
          console.log "</ul></li>"
      else if stat && stat.isFile()
        console.log "<li class='file' data-path='#{dir}' data-filename='#{dirname}'><span>#{dirname}</span></li>"

  return callback

As you can see, I have tried to give the function a callback but when I use it 如您所见,我尝试给该函数回调,但是当我使用它时

listFolders path, () ->
  console.log "</ul></li>"

the console log is never run, nor did it pause my code. 控制台日志永远不会运行,也不会暂停我的代码。

My output of the folder I am scanning is 我正在扫描的文件夹的输出是

<li class='file' data-path='/user_projects/1' data-filename='.DS_Store'><span>.DS_Store</span></li>
<li class='folder' data-path='/user_projects/1' data-name='/user_projects/1/assets'><span>assets</span><ul>
<li class='file' data-path='/user_projects/1' data-filename='index.html'><span>index.html</span></li>
<li class='folder' data-path='/user_projects/1/assets' data-name='/user_projects/1/assets/css'><span>css</span><ul>
<li class='file' data-path='/user_projects/1/assets' data-filename='test.php'><span>test.php</span></li>
<li class='file' data-path='/user_projects/1/assets/css' data-filename='main.css'><span>main.css</span></li>

and it should be 它应该是

<li class='file' data-path='/user_projects/1' data-filename='.DS_Store'><span>.DS_Store</span></li>
<li class='folder' data-path='/user_projects/1' data-name='/user_projects/1/assets'><span>assets</span><ul>
    <li class='folder' data-path='/user_projects/1/assets' data-name='/user_projects/1/assets/css'><span>css</span><ul>
       <li class='file' data-path='/user_projects/1/assets' data-filename='test.php'><span>test.php</span></li>
       <li class='file' data-path='/user_projects/1/assets/css' data-filename='main.css'><span>main.css</span></li>
    </ul></li>
</ul></li>
<li class='file' data-path='/user_projects/1' data-filename='index.html'><span>index.html</span></li>

I've been suffering with this problem for a while now and would like someone to help me out. 我已经忍受了这个问题一段时间了,希望有人帮我。 Your help will be very much appreciated. 非常感谢您的帮助。

>>>>>>UPDATE USING forEachSeries<<<<<<< >>>>>>使用forEachSeries的更新<<<<<<<

I am now using the code provided below 我现在正在使用下面提供的代码

listFolders = (dir, callback) ->
  readDir = "#{__dirname}/public/#{dir}"
  fs.readdir readDir, (err, files) ->
    async.forEachSeries files, (file, callback) ->
      dirname = file
      path = "#{dir}/#{file}"
      readPath = "#{readDir}/#{file}"

      fs.stat readPath, (err, stat) ->
        if stat && stat.isDirectory()
          console.log "<li class='folder'><span>#{dirname}</span><ul>"
          listFolders path, () ->
            console.log "</ul></li>"
            callback()
        else if stat && stat.isFile()
          console.log "<li class='file'><span>#{dirname}</span></li>"
          callback()
  , callback

and my output is not ant I'd like but instead this (closing 'ul' and 'li' not showing): 而且我的输出不是我想要的蚂蚁,而是这样(不显示“ ul”和“ li”):

<li class='file' data-path='/user_projects/1' data-filename='.DS_Store'><span>.DS_Store</span></li>
<li class='folder'><span>assets</span><ul>
    <li class='folder'><span>css</span><ul>
        <li class='file'><span>main.css</span></li>

my folders in the directory are and should be displayed like 我在目录中的文件夹应该并且应该显示为

- .ds_store
- asstets
  - css
    - main.css
  - test.php
- index.html

You're passing in a function callback , but you're never calling it. 您传递的是函数callback ,但从未调用它。 You're just returning it. 您只是要退货。 You may want to look into something like async.js . 您可能需要研究async.js之类的东西。 Then you would write code that looks something like: 然后,您将编写类似于以下内容的代码:

listFolders = (dir, callback) ->
  readDir = "#{__dirname}/public/#{dir}"
  fs.readdir readDir, (err, files) ->
    async.forEachSeries files, (file, callback) ->
      dirname = file
      path = "#{dir}/#{file}"
      readPath = "#{readDir}/#{file}"

      fs.stat readPath, (err, stat) ->
        if stat && stat.isDirectory()
          console.log "<li class='folder' data-path='#{dir}' data-name='#{dir}/#{dirname}'><span>#{dirname}</span><ul>"
          listFolders path, () ->
            console.log "</ul></li>"
            callback()
        else if stat && stat.isFile()
          console.log "<li class='file' data-path='#{dir}' data-filename='#{dirname}'><span>#{dirname}</span></li>"
          callback()
  , callback

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM