Skip to content

Commit 6bd9c06

Browse files
kajmagnusLeaVerou
authored andcommitted
Expose xhr.abort() from Bliss.fetch(). (#199)
* Expose xhr.abort() from Bliss.fetch(). * Edit comment and docs.
1 parent 986b620 commit 6bd9c06

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

bliss.shy.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ extend($, {
371371
env.xhr.setRequestHeader(header, env.headers[header]);
372372
}
373373

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

@@ -401,6 +401,9 @@ extend($, {
401401

402402
env.xhr.send(env.method === "GET"? null : env.data);
403403
});
404+
// Hack: Expose xhr.abort(), by attaching xhr to the promise.
405+
promise.xhr = env.xhr;
406+
return promise;
404407
},
405408

406409
value: function(obj) {

docs.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1218,7 +1218,9 @@ <h1>fetch</h1>
12181218
<dd>A promise that is resolved when the resource is successfully fetched and rejected if there is any error.
12191219
When the request is successful, the promise resolves with the XHR object.
12201220
When the request fails, the promise rejects with an Error whose message is a description of the error.
1221-
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>
1221+
The error object also contains two properties: <code>xhr</code> which points to the XHR object, and
1222+
<code>status</code> which returns the status code (e.g. 404). In case you need to abort the request,
1223+
the xhr is returned in a field on the promise, so you can: <code>promise.xhr.abort()</code>.</dd>
12221224
</dl>
12231225

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

0 commit comments

Comments
 (0)