Iron Ajax promise

How can I write Promise where the answer comes from 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 response);
            });
        }
    }
};
Run codeHide result
+6
source share
1 answer

You are looking listItem.generateRequest(), as this returns iron-ajax, the accompanying iron request object, which in turn makes a promise with a name 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 sample code 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;
        }
    }
};
Run codeHide result

Inspired by a more complex example of fooobar.com/questions/1016262 / ... . Kudos to @akivajgordon.

+7

Source: https://habr.com/ru/post/1016260/


All Articles