【问题标题】:Making sync call using callbacks/promises/other inside for loop in Javascript在Javascript中的for循环中使用回调/承诺/其他进行同步调用
【发布时间】:2017-10-02 14:44:02
【问题描述】:

我有以下情况:

我写了一个appendToFile() 函数:

function appendToFile(text, file) {
        (function (e) {
            freeze();
            FileUtils.readAsText(file)
                .done(function (data) {
                    console.log("File data: " + data);
                    console.log("File text: " + e);
                    var text = data + e;
                    FileUtils.writeText(file, text, true)
                        .done(function () {
                            console.log("File saved. Text: " + text);
                            unfreeze();
                            //window.alert("File saved. Text: " + text);
                        })
                        .fail(function (err) {
                            console.error(err);
                            window.alert(err);
                        });
                })
                .fail(function (err) {
                    console.error(err);
                });
        })(text);
    }

此函数需要将一些文本附加到文件中。从这个函数调用它:

function processAttrs(clazz) {
        //window.alert("Hello" + clazz.name + " " + clazz.length);
        var file = FileSystem.getFileForPath("/Users/XXX/" + clazz.name + ".txt");
        createFile(file, function () {
            for (var i=0; i<clazz.attributes.length; i++) {
                var attr = clazz.attributes[i];
                var text = "private " + attr.type + " " + attr.name + "\n";
                appendToFile(text, file);
            }
        });
    }

问题是 FileUtils.readAsText(file) 是异步调用的,所以一些需要写入文件的行会丢失。

freeze() 和 unfreeze() 只是空函数,我想在其中实现一些东西来停止和恢复代码执行,但这种机制在 Javascript 中似乎不存在。

我考虑过像使用 createFile(file) 函数一样使用回调。问题是我不知道将什么作为回调函数传入,因为代码执行在 for 循环内。

我可以想到没有 for 循环的解决方案,并使用回调手动处理流程,但我不喜欢它。必须有一个更优雅的解决方案

【问题讨论】:

    标签: javascript for-loop asynchronous callback promise


    【解决方案1】:

    有几种方法可以解决这个问题。您可以使用async/await 所以你可以“阻止”你的循环

    function appendToFile(text, file) {
        return new Promise((resolve, reject) => {
            (function (e) {
                //...
            })(text);
        });
    }
    
    async function  processAttrs(clazz) {
        //window.alert("Hello" + clazz.name + " " + clazz.length);
        var file = FileSystem.getFileForPath("/Users/XXX/" + clazz.name + ".txt");
        createFile(file, async function () {
            for (var i=0; i<clazz.attributes.length; i++) {
                var attr = clazz.attributes[i];
                var text = "private " + attr.type + " " + attr.name + "\n";
                const result =  await appendToFile(text, file);
            }
        });
    }
    

    或者像这里显示的某种承诺瀑布Is Node.js native Promise.all processing in parallel or sequentially?

    【讨论】:

      【解决方案2】:

      更改您的代码并使用async/await 语法,如下所示。

      async function appendToFile(text, file) {
          try {
              freeze();
              var data = await FileUtils.readAsText(file)
              console.log("File data: " + data);
              console.log("File text: " + text);
              var text = data + text;
              await FileUtils.writeText(file, text, true)
              console.log("File saved. Text: " + text);
              unfreeze();
              return;
          } catch (e) {
              console.log('Error', e)
          }
      }
      
      function processAttrs(clazz) {
          var file = FileSystem.getFileForPath("/Users/XXX/" + clazz.name + ".txt");
          createFile(file, async function () {
              for (var attr of clazz.attributes) {
                  var text = "private " + attr.type + " " + attr.name + "\n";
                  await appendToFile(text, file) //This will stop the loop until each file is completed
              }
          })
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-10-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-29
        • 2016-10-11
        • 1970-01-01
        • 2015-12-18
        相关资源
        最近更新 更多