简体   繁体   English

这是缩进JavaScript中嵌套回调的推荐方法吗?

[英]Is this the recommended way to indent nested callbacks in Javascript?

I've seen in some APIs documentation that they use the following syntax for nested callbacks: 我在某些API文档中看到,它们对嵌套回调使用以下语法:

webmap = new WebMap({
  portalItem: {
    id: "id"
  }
})
    .load()
        .then(function(instance){
            console.log(instance);
            view = new MapView({
              map: instance,
              container: "map"
            })
                .then(function(instance){

                })
            ;
        })
    ;
;

Is this the recommended way to syntax nested calls in Javascript? 这是在Javascript中对嵌套调用进行语法化的推荐方法吗?

Also, why? 还有,为什么呢? As someone who comes from Python, it seems so weird and unnecessary. 作为来自Python的人,这似乎很奇怪而且没有必要。

If anyone is curious, I've seen this way of identation mostly in Semantic Ui's examples https://semantic-ui.com/modules/sidebar.html#/examples 如果有人好奇,我主要在Semantic Ui的示例中看到过这种身份识别方法https://semantic-ui.com/modules/sidebar.html#/examples

When you have deeply nested then calls, you might want to check if that is really necessary. 当您深深嵌套then调用时,您可能需要检查这是否确实必要。 In your case it is not. 在您的情况下不是。 Move the inner then calls to the outer chain: 移动内部then调用外部链:

webmap = new WebMap({
    portalItem: {
        id: "id"
    }
}).load().then(function(instance){
    console.log(instance);
    // return (!) the promise, instead of applying a nested `then`
    return new MapView({
        map: instance,
        container: "map"
    });
}).then(function(instance){ // the `then` is now on the outer chain.

});

This way the depth of indentation remains reasonable, which is one of the advantages of promises (when you use them well). 这样,缩进的深度保持合理,这是promise的优点之一(当您很好地使用它们时)。

You're using a Promise interface (or something similar to it) so you aren't actually using nested callbacks in the traditional sense - though I see your .then() after the MapView construction would count as one. 您正在使用Promise接口(或类似的接口),因此实际上并没有使用传统意义上的嵌套回调-尽管在MapView构造后我看到您的.then()会算作一个。

The then invocations for the same sequence of events should be at the same indent level, a nested sequence of events should still have all of its events at the same level. then ,对相同事件序列的调用应在相同的缩进级别上,嵌套的事件序列应仍将其所有事件都在同一级别上。 This is how I would format it: 这是我格式化的方式:

webmap = new WebMap( {
        portalItem: {
            id: "id"
        }
    } )
    .load()
    .then( function( instance ) {
        console.log( instance );
        view = new MapView( {
            map: instance,
            container: "map"
        } )
        .then( function( instance ) {

        } );
    } );

Note you can make this more succinct by using the arrow-function syntax: 请注意,您可以使用箭头功能语法来使其更加简洁:

webmap = new WebMap( {
        portalItem: {
            id: "id"
        }
    } )
    .load()
    .then( instance => {
        console.log( instance );
        view = new MapView( {
            map: instance,
            container: "map"
        } )
        .then( instance => {

        } );
    } );

Readability could be improved if you move the anonymous options objects out of the sequence of events, but as the MapView constructor's options takes the instance value that's not an option in this case. 如果将匿名options对象移出事件序列,则可以提高可读性,但是由于MapView构造函数的options采用instance值,因此在这种情况下不是选项。

But for the WebMap constructor you can: 但是对于WebMap构造函数,您可以:

var webMapOption = { portalItem: { id: "id" } };
webMap = new WebMap( webMapOptions )
    .load()
    .then( instance => ... );

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

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