简体   繁体   中英

A promise with iron-ajax response

How can I write a Promise where the response is coming from an iron-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); }); } } }; 

You're looking for listItem.generateRequest() , since that returns iron-ajax's accompanying iron-request object, which in turn provides a promise, named 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

I modified your code sample below:

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

Inspired by the more complex example at https://stackoverflow.com/a/37995462/2795627 . Kudos to @akivajgordon.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM