Skip to content

Commit

Permalink
fix resolve twice bug
Browse files Browse the repository at this point in the history
  • Loading branch information
camsong committed Dec 9, 2015
1 parent 0567552 commit 3a04845
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -342,12 +342,15 @@
return;
}

var __onLoadHandled = false;

function onload() {
if (xhr.readyState !== 4) {
return
}
var status = (xhr.status === 1223) ? 204 : xhr.status
if (status < 100 || status > 599) {
if (__onLoadHandled) { return; } else { __onLoadHandled = true; }
reject(new TypeError('Network request failed'))
return
}
Expand All @@ -358,11 +361,14 @@
url: responseURL()
}
var body = 'response' in xhr ? xhr.response : xhr.responseText;

if (__onLoadHandled) { return; } else { __onLoadHandled = true; }
resolve(new Response(body, options))
}
xhr.onreadystatechange = onload;
xhr.onload = onload; // FIX: may fire two times
xhr.onload = onload;
xhr.onerror = function() {
if (__onLoadHandled) { return; } else { __onLoadHandled = true; }
reject(new TypeError('Network request failed'))
}

Expand Down

0 comments on commit 3a04845

Please sign in to comment.