From 3a04845fa3eb1a61fc5e8f746c9fd1c83502c29e Mon Sep 17 00:00:00 2001 From: Cam Song Date: Wed, 9 Dec 2015 21:02:15 +0800 Subject: [PATCH] fix resolve twice bug --- fetch.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/fetch.js b/fetch.js index cb4604de..79f89409 100644 --- a/fetch.js +++ b/fetch.js @@ -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 } @@ -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')) }