Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 625
Estus Flask Avatar asked Dec 09 '22 02:12

Estus Flask


2 Answers

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.

like image 169
loganfsmyth Avatar answered Jan 03 '23 15:01

loganfsmyth


Since System.import returns a promise, use a group of promises. I find it quite a bit more straight forward than chaining.

Promise.all([
    System.import('core-js'),
    System.import('polymer/mutationobservers'),
    System.import('aurelia-bootstrapper')
]).then(function(modules) {
    var corejs = modules.shift(),
        mutationobservers = modules.shift(),
        aureliaBootstrapper = modules.shift()
    ;

    // You code goes here.
});
like image 21
vangorra Avatar answered Jan 03 '23 15:01

vangorra