如何解决JavaScript模块取决于异步函数返回值的结果
对于我来说,以下是一个非常常见的模块模式,我要构建的特定模块的不同之处在于,它依赖于REST API调用返回的值。接线该代码,以便“ siteinfo”可用于模块中的其他功能以及调用模块中的功能的正确方法是什么。
mod1.getSiteInfo()返回正确的值,只是不是立即返回。
(function (ns) {
var siteinfo;
jQuery.getJSON(location.href + '/_api/site')
.then( function(data) {
siteinfo = data;
});
ns.getSiteInfo = function() {
return siteinfo
}
// TODO: need to write a dozen other methods that rely on ns.siteinfo having proper data from the API call.
}(window.mod1 = window.mod1 || {}));
// and how to know that getSiteInfo will provide me the value i expect from the API Call?
console.log(mod1.getSiteInfo());
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。