How do I recover due to an error in the dojo plugin?

I have a requirement when I need to pass a list of modules to a plugin and load it and do some work. If I have been given a module that I cannot load, I must report the error and go to the rest of the list. I am stuck because I can’t figure out how to recover due to a failure in a failed module. Is there any other technique that I can use to meet this requirement? Here is an example that solves the problem without all my other requirements, I need to recover from failure to load my / thing2:

define("my/thing", [], function() { return 'thing'; }); define("my/loader", [], function() { return { load: function(mid, require, callback) { console.log('inside load', arguments); // is there some way to recover when this require fails // or some other technique I can use here? try { require([mid], function(mod) { console.log('inside require, mod=', mod); callback(mod); }); } catch (error) { // never gets here, when the require fails everything just stops console.log(error); callback("failed to load " + mid); } } } }); require(["my/loader!my/thing"], function(loaded) { console.log('loaded', loaded); }); require(["my/loader!my/thing2"], function(loaded) { console.log('loaded', loaded); }); 
+4
source share
1 answer

If you strictly need to ignore invalid or faulty modules and move on to the next, use dojo / _ base / lang :: exists () before throwing them into the require statement:

 require(['/dojo/_base/lang', 'dojo/text!my/thing2'], function(lang, myThing2) { if(lang.exists(myThing2)) { //success } else { //failure } }); 
+1
source

Source: https://habr.com/ru/post/1502140/


All Articles