如何在响应来自铁ajax的地方写一个Promise。
<iron-ajax id="listItems" method="GET" content-type="application/json" handle-as="json" last-response="{{items}}" on-error="handleErrorResponse"></iron-ajax>
this.data = {
get: function(sort, page, pageSize) {
return new Promise(function(resolve, reject) {
// Execute iron-ajax.
//...
// resolve(iron-ajax's response);
});
}
}
};
答案 0 :(得分:8)
你正在寻找listItem.generateRequest()
,因为它返回了铁-ajax伴随的铁请求对象,后者又提供了一个名为request.completes
的承诺。
https://www.webcomponents.org/element/PolymerElements/iron-ajax/iron-ajax#method-generateRequest
https://www.webcomponents.org/element/PolymerElements/iron-ajax/iron-request#property-completes
我修改了下面的代码示例:
<iron-ajax id="listItems" method="GET" content-type="application/json" handle-as="json" last-response="{{items}}" on-error="handleErrorResponse"></iron-ajax>
this.data = {
get: function(sort, page, pageSize) {
return this.$.listItems.generateRequest().completes;
}
}
};
受到https://stackoverflow.com/a/37995462/2795627更复杂的例子的启发。感谢@akivajgordon。