【问题标题】:How do i restructure following code? [closed]我如何重组以下代码? [关闭]
【发布时间】:2018-06-27 19:59:34
【问题描述】:

我想创建一个 json 结构,其中包含从 api 调用中获取的数据。我可以使用以下代码生成结构。但是如何重构代码以删除函数和循环的嵌套调用。

 var temp = {
    applications: []
};
    api.getApplications(conceptId)
        .then((applications) => {
            for (var i = 0; i < applications.length; i++) {
                (function(indexOfAppArr) {
                    let applicationId = applications[indexOfAppArr].id;
                    temp.applications.push({
                        id: applicationId,
                        databases: []
                    });
                    api.getDbs(conceptId, applicationId)
                        .then(databases => {
                            for (var j = 0; j < databases.length; j++) {
                                (function(indexOfDatabasArr) {
                                    let databaseid = databases[indexOfDatabasArr].id;
                                    temp.applications[indexOfAppArr].databases.push({
                                        id: databaseid,
                                        tabels: []
                                    });
                                    api.
                                    getSchema(conceptId,
                                        applicationId, databaseid).
                                    then(function(schemas) {
                                        for (var k = 0; k < schemas.length; k++) {
                                            (function(indexofschemaarr) {
                                                let schemaid = schemas[indexofschemaarr].id;
                                                api.getTable(conceptId, schemaid)
                                                    .then(function(tables) {
                                                        console.log(tables);
                                                    })
                                            })(k)
                                        }
                                    })
                                })(j)
                            }
                        })
                })(i)
            }
        })

这是我要创建的 JSON 结构。

    {
    applications:[{
        id:'',
        databases:[{
            id:'',
            tabels:[
                {
                    id:'',
                    columnId:''
                }
            ]   
        }]
    }]
};

【问题讨论】:

  • 你可以从阅读承诺链开始:)

标签: javascript json node.js


【解决方案1】:

如果您阅读一些内容,您将真正学会如何去做。我个人还没有必要学习它,但听起来很有趣,这是我为你找到的一个很棒的网站:

https://javascript.info/promise-chaining

它解释了如何通过用更少的文字来“重组”您所询问的代码:

loadScript("/article/promise-chaining/one.js").then(function(script1) {
  loadScript("/article/promise-chaining/two.js").then(function(script2) {
    loadScript("/article/promise-chaining/three.js").then(function(script3) {
      // this function has access to variables script1, script2 and script3
      one();
      two();
      three();
    });
  });
});

我确信它只需要不到 30 米的阅读时间。祝你好运!

【讨论】:

    猜你喜欢
    • 2017-03-19
    • 2015-06-06
    • 1970-01-01
    • 1970-01-01
    • 2021-09-05
    • 2020-09-10
    • 1970-01-01
    • 1970-01-01
    • 2011-10-29
    相关资源
    最近更新 更多