0

我将这一点代码与 node.js 混合在一起,它允许我创建目录的无序列表。我想要的是父循环函数暂停并等待子循环完成,然后继续执行代码。

这是我的代码:

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

如您所见,我试图给函数一个回调,但是当我使用它时

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

控制台日志从未运行,也没有暂停我的代码。

我正在扫描的文件夹的输出是

<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>

它应该是

<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>

我已经被这个问题困扰了一段时间,希望有人能帮助我。您的帮助将不胜感激。

>>>>>>使用 forEachSeries 进行更新<<<<<<<

我现在正在使用下面提供的代码

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

我的输出不是我想要的蚂蚁,而是这个(关闭'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>

我在目录中的文件夹是并且应该显示为

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

1 回答 1

0

你传入一个函数callback,但你从来没有调用它。你只是退货。您可能想查看类似async.js的内容。然后,您将编写类似于以下内容的代码:

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
于 2013-10-16T17:13:34.813 回答