Accepted practice for an ES6 module imported just for side-effects?

后端 未结 2 1350
长发绾君心
长发绾君心 2021-01-13 05:20

I like to keep my code modular, so I put this kind of code in a separate file (overrides/extra.js):

import Ember from \'ember\';

Ember.RSVP.con         


        
相关标签:
2条回答
  • 2021-01-13 06:03

    Yes this is accepted if your module doesn't need to export any data, but there's no need to export anything from a module if it's not required:

    import Ember from 'ember';
    
    Ember.RSVP.configure('onerror', function(error) {
        ....
    });
    

    app.js:

    import './overrides/extra';
    
    0 讨论(0)
  • I usually export an initialization function:

    • the calling code is more explicit
    • the calling code is able to take action on success (or failure)

    I don't know Ember and its initialization but here's how it could look like:

    import Ember from 'ember';
    
    export default function initialize(){
        return new Promise(function(ok, nok){
            try { // or any other kind of failure detection
                Ember.RSVP.configure('onerror', function(error) {
                    ok();
                });
                ... you may do other initializations here
                ok();
            } catch (e) {
                nok(e);
            }
        });
    }
    

    In the calling code

    import initEmber from './yourFile.js';
    
    initEmber()
    .then(function(){
        ... here you know Ember is ready
    })
    .catch(function(){
        ... damn it!
    });
    

    Note that if Ember initialization is synchronous and may not fail, @RGraham answer may suit you better.

    0 讨论(0)
提交回复
热议问题