在触发单个函数之前,等待所有.then()执行完毕
Wait for all .then() to be executed before firing a single function
我试图在加载所有模板时触发一个函数。这是通过每个模板的loadTemplate函数完成的。
我的问题是如何侦听所有的。then()完成,只有在那之后才触发我的foo()函数。
我试过使用when()和promise,但我没有成功地将其绑定到外部,而我已经设法将其绑定到loadTemplate(这不是我要找的)。
我总是可以将每个loadTemplate绑定到一个计数器,并在每次迭代时检查计数器是否已达到每个模板上的模板数量,但我确信有一种更优雅的方法可以做到这一点。
下面是循环代码:
$.each(templates.hbs, function(idx, template) {
loadTemplate(template.name)
.then(function(templateStr){
var compiledTemplate = Handlebars.compile(templateStr);
if(template.isPartial){
Handlebars.registerPartial(template.key, compiledTemplate);
}
templates[template.key] = compiledTemplate;
});
});
和之后触发的函数:
function templateDone(){
console.log("done!");
}
谢谢你的帮助!
编辑:包括loadTemplate代码以及:
function loadTemplate (template) {
return new Promise(function (resolve, reject) {
$.ajax({
url: config.wwwroot + '/folder/' + template + '.hbs',
async: false,
success: function (result) {
resolve(result);
},
error: reject
});
});
}
loadTemplate函数返回一个promise,你可以使用。when()。请参阅文档示例,了解等待多个承诺解决的情况。
你可以这样做:
var promises = [];
$.each(templates.hbs, function (idx, template) {
promises.push(loadTemplate(template.name));
});
$.when.apply($, promises).then(templateDone);
至于你的loadTemplate函数,你不需要创建你自己的promise。美元。Ajax默认返回一个承诺。
function loadTemplate(template) {
return $.ajax({
/* ... */
});
}
看看这是否有影响?也可以尝试不使用async属性。我不知道为什么会这样,但值得一试。
您可以编写简单的条件来检查是否所有模板都像这样加载
var loaded_template = 0;
$.each(templates.hbs, function(idx, template) {
loadTemplate(template.name)
.then(function(templateStr){
var compiledTemplate = Handlebars.compile(templateStr);
if(template.isPartial){
Handlebars.registerPartial(template.key, compiledTemplate);
}
templates[template.key] = compiledTemplate;
loaded_template++;
if(loaded_template == templates.hbs.length){
foo();
}
});
});
我希望这对你有用。即使使用ajax调用加载模板,也可以使用这种方法。$后调用函数。
相关文章:
- 无法在通过jQuery的ajax加载的页面中执行javascript
- JavaScript执行暂时挂起页面
- 如何在ReactJS JSX中执行嵌套的if-else语句
- 当js函数's已执行
- 哪个先执行
- 在使用Polymer'加载所有json文件后执行方法;s的核心ajax
- HTML表单提交时未执行外部函数
- 如何重写这段代码,使执行不必从Javascript Promise.then()方法开始
- ajax-返回并防止延迟.then()执行
- 类似于jQuery when/then的东西:带break的延迟执行
- 正在从.then()函数执行命名的“胖箭头”
- 基本if/then JS/JQuery-为什么这似乎忽略了if并执行了两次
- 的承诺.在执行.then()之前不等待解析
- 在触发单个函数之前,等待所有.then()执行完毕
- 我需要把'return'casper.then'块,让父函数等待子函数完成执行
- 当我需要调用者的执行暂停/等待时,如何使用promise.then() ?
- 为什么Mocha不执行.then语句中的测试?
- Promises是调用`.then`后执行副作用所需的A+Promises吗
- nodejs then() 函数在 promise 解析之前执行
- 链式jQuery.then() -每个下一个只在前一个解析完成后执行