我正在使用 RequireJS 来构建我当前的 JS 项目,并且我有一些库/jQuery 插件需要在使用之前进行初始化。初始化是异步的,返回时将调用回调。因此,以下代码引入了竞争条件(依赖项是否已加载和初始化?):
dep_that_needs_to_be_initialized_before_it_can_be_used.js:
define(
    ['raw_library_source'],
    function(){
        // this init call is async and will invoke the callback when done
        // $ is global
        $.plugin.init({
            //config
        },callback);
    }
);
应用程序.js:
define(
    ['dep_that_needs_to_be_initialized_before_it_can_be_used'],
    function(){
        // the following line may fail in case the dependcy is not yet initialzed
        $.plugin.doSomething();
    }
);
有谁知道我如何使依赖项“等待”(又名同步)直到库也被初始化?