Search code examples
javascriptpromiseecmascript-6systemjs

System.import promise chaining


I've stumbled upon some snippets similar to this one:

  System.import('core-js').then( function() {
    System.import('polymer/mutationobservers').then( function() {
      System.import('aurelia-bootstrapper');
    })
  });

Is it a substitution for callback hell - a promise hell? Can sequential System.imports be flattened to make use of promise chaining, or there may be problems with that?


Solution

  • I'd recommend chaining instead, e.g.

    System.import('core-js')
        .then(function(){
            return System.import('polymer/mutationobservers');
        })
        .then(function(){
            return System.import('aurelia-bootstrapper');
        });
    

    When you return a promise from a then, it will wait for that to resolve before executing the next then, so in this case mutationobservers must load before aurelia-bootstrapper.