我有一个用例,其中Singleton对象具有异步步骤作为其初始化的一部分。此单例的其他公共方法取决于初始化步骤设置的实例变量。我如何进行异步调用同步?
var mySingleton = (function () {
var instance;
function init() {
// Private methods and variables
function privateMethod(){
console.log( "I am private" );
}
var privateAsync = (function(){
// async call which returns an object
})();
return {
// Public methods and variables
publicMethod: function () {
console.log( "The public can see me!" );
},
publicProperty: "I am also public",
getPrivateValue: function() {
return privateAsync;
}
};
};
return {
// Get the Singleton instance if one exists
// or create one if it doesn't
getInstance: function () {
if ( !instance ) {
instance = init();
}
return instance;
}
};
})();
var foo = mySingleton.getInstance().getPrivateValue();
答案 0 :(得分:3)
如果你真的想使用IIFE创建一个类似于单例的方法,你仍然必须使用带异步调用的promises或回调,并使用它们,而不是尝试将异步转换为同步
像
这样的东西var mySingleton = (function() {
var instance;
function init() {
// Private methods and variables
function privateMethod() {
console.log("I am private");
}
var privateAsync = new Promise(function(resolve, reject) {
// async call which returns an object
// resolve or reject based on result of async call here
});
return {
// Public methods and variables
publicMethod: function() {
console.log("The public can see me!");
},
publicProperty: "I am also public",
getPrivateValue: function() {
return privateAsync;
}
};
};
return {
// Get the Singleton instance if one exists
// or create one if it doesn't
getInstance: function() {
if (!instance) {
instance = init();
}
return instance;
}
};
})();
var foo = mySingleton.getInstance().getPrivateValue().then(function(result) {
// woohoo
}).catch(function(err) {
// epic fail
})