kubetail-org / loadjs

A tiny async loader / dependency manager for modern browsers (899 bytes)
MIT License
2.57k stars 149 forks source link

Why not execute success callback instead of throw error if bundle is already defined? #72

Closed bolasblack closed 6 years ago

bolasblack commented 6 years ago

The code is here:

https://github.com/muicss/loadjs/blob/b1795cf94a3429619c3dfe947fcb303c6d4e6b65/src/loadjs.js#L217-L224

This behavior looks so weird to me.

Is there any reason for doing this?

amorey commented 6 years ago

That code path is reached if a user tries to define a new bundle using an old name. If you want to add a new success callback to a previously-defined bundle you can use the .ready() method:

// define bundle and add a success callback
loadjs(['/file1.js', 'file2.js'], 'mybundle', function() {
  /* success-callback-1 */
});

// add another success callback
loadjs.ready('mybundle', function() {
  /* success-callback-2 */
});
bolasblack commented 6 years ago

Got it :P

Thanks