【发布时间】:2016-11-02 18:14:14
【问题描述】:
这是现在的代码,promise 链和以前一样。我已经开始尝试使用 Promise.all() 来摆脱反模式
(function(){
'use strict';
function DOMObj(){
var that = this;
that.items = [];
that.getitems = function(url) {
return new Promise (resolve => {
$.getJSON(url, function (response) {
for (var i = 0; i < response.sales.length; i++) {
that.items.push(new ProductObj(response.sales[i], i));
}
resolve();
});
}
)
};
that.updateProductHTML = function(){
return new Promise(resolve => {
for( var i = 0; i < that.items.length; i++){
that.items[i].updateHTML();
}
resolve();
})
};
that.updateDOM = function() {
return new Promise(resolve => {
var thisHTML = '';
for( var i = 0; i < that.items.length; i++) {
if (i % 3 === 0 ) {
thisHTML += "<div class='row'>";
// console.log("START");
}
thisHTML += that.items[i].htmlView;
if ((i % 3 === 2) || i === (that.items.length - 1) ) {
thisHTML += "</div>";
console.log("FINISH");
}
}
$("#content").append(thisHTML);
resolve();
})
}
}
function ProductObj(product, i) {
var that = this;
that.photo = product.photo;
that.title = product.title;
that.subtitle = product.subTitle;
that.url = product.url;
that.htmlView = "";
that.index = i;
that.updateHTML = function() {
$.get('template.html', function(template){
that.htmlView = template.replace('{image}', that.photo)
.replace('{title}', that.title)
.replace('{subtitle}', that.subtitle)
.replace('{url}', that.url);
console.log(that.index + ' product has worked through html')
})
};
}
var myView = new DOMObj();
myView.getitems('data.json')
.then(
myView.updateProductHTML
)
.then(
myView.updateDOM()
)
.then(() =>{
$('.deleteButton').click(() => {
$(this).parent().remove();
});
//Promise.all([ myView.getitems('data.json'), myView.updateProductHTML, myView.updateDOM, () => {$('.deleteButton').click(() => {
//$(this).parent().remove();
//})}])
})();
到目前为止,代码按此顺序运行 getItems => updateProductHTML 然后 updateDOM 运行,我尝试添加的最后一个代码是按钮上的单击事件,显然需要最后运行
【问题讨论】:
-
请向我们展示您的实际代码。不要说“我的承诺是这样设置的”,然后向我们展示可怕的损坏(伪?)代码。
-
this.updateHTML看起来是异步的,是的,但它实际上需要return$.get承诺才有用! -
那是代码跳过者
-
好吧,但这是您遇到问题的代码,所以请不要跳过它。
-
不是实际代码
标签: javascript jquery ecmascript-6