From 600566762ae2ecdcf385b09f08006feee1b098b7 Mon Sep 17 00:00:00 2001 From: David Graham Date: Tue, 14 Oct 2014 20:46:56 -0600 Subject: [PATCH] Don't expose consumed function as public Body API. --- fetch.js | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/fetch.js b/fetch.js index 7e1f357..f1818cb 100644 --- a/fetch.js +++ b/fetch.js @@ -61,6 +61,13 @@ }) } + function consumed(body) { + if (body.bodyUsed) { + return new Promise.reject(new TypeError('Body already consumed')) + } + body.bodyUsed = true + } + function Body() { this.body = null this.bodyUsed = false @@ -70,7 +77,7 @@ } this.blob = function() { - var error = this.consumed() + var error = consumed(this) return error ? error : Promise.resolve(new Blob([this.body])) } @@ -79,7 +86,7 @@ } this.json = function() { - var error = this.consumed() + var error = consumed(this) if (error) { return error } @@ -95,17 +102,10 @@ } this.text = function() { - var error = this.consumed() + var error = consumed(this) return error ? error : Promise.resolve(this.body) } - this.consumed = function() { - if (this.bodyUsed) { - return new Promise.reject(new TypeError('Body already consumed')) - } - this.bodyUsed = true - } - return this } -- GitLab