铁的承诺-ajax 回应

A promise with iron-ajax response

我如何写一个 Promise,其中响应来自铁-ajax。

<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);
            });
        }
    }
};

您正在寻找 listItem.generateRequest(),因为那个 returns iron-ajax 伴随的 iron-request 对象,它又提供了一个名为 [=12= 的承诺].

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;
        }
    }
};

受 中更复杂示例的启发。感谢@akivajgordon。