Skip to content

Commit

Permalink
Expose xhr.abort() from Bliss.fetch(). (#199)
Browse files Browse the repository at this point in the history
* Expose xhr.abort() from Bliss.fetch().

* Edit comment and docs.
  • Loading branch information
kajmagnus authored and LeaVerou committed Jul 19, 2017
1 parent 986b620 commit 6bd9c06
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
5 changes: 4 additions & 1 deletion bliss.shy.js
Expand Up @@ -371,7 +371,7 @@ extend($, {
env.xhr.setRequestHeader(header, env.headers[header]);
}

return new Promise(function(resolve, reject) {
var promise = new Promise(function(resolve, reject) {
env.xhr.onload = function() {
document.body.removeAttribute("data-loading");

Expand Down Expand Up @@ -401,6 +401,9 @@ extend($, {

env.xhr.send(env.method === "GET"? null : env.data);
});
// Hack: Expose xhr.abort(), by attaching xhr to the promise.
promise.xhr = env.xhr;
return promise;
},

value: function(obj) {
Expand Down
4 changes: 3 additions & 1 deletion docs.html
Expand Up @@ -1218,7 +1218,9 @@ <h1>fetch</h1>
<dd>A promise that is resolved when the resource is successfully fetched and rejected if there is any error.
When the request is successful, the promise resolves with the XHR object.
When the request fails, the promise rejects with an Error whose message is a description of the error.
The error object also contains two properties: <code>xhr</code> which points to the XHR object, and <code>status</code> which returns the status code (e.g. 404).</dd>
The error object also contains two properties: <code>xhr</code> which points to the XHR object, and
<code>status</code> which returns the status code (e.g. 404). In case you need to abort the request,
the xhr is returned in a field on the promise, so you can: <code>promise.xhr.abort()</code>.</dd>
</dl>

<pre><code>$.fetch("/api/create", {
Expand Down

0 comments on commit 6bd9c06

Please sign in to comment.