Skip to content

Commit

Permalink
Extract consumed function.
Browse files Browse the repository at this point in the history
  • Loading branch information
dgraham committed Oct 15, 2014
1 parent c7a27dc commit a709976
Showing 1 changed file with 10 additions and 12 deletions.
22 changes: 10 additions & 12 deletions fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,25 +70,20 @@
}

this.blob = function() {
if (this.bodyUsed) {
return new Promise.reject(new TypeError('Body already consumed'))
}

this.bodyUsed = true
return Promise.resolve(new Blob([this.body]))
var error = this.consumed()
return error ? error : Promise.resolve(new Blob([this.body]))
}

this.formData = function() {
throw new Error('Not implemented yet')
}

this.json = function() {
if (this.bodyUsed) {
return new Promise.reject(new TypeError('Body already consumed'))
var error = this.consumed()
if (error) {
return error
}

this.bodyUsed = true

var body = this.body
return new Promise(function(resolve, reject) {
try {
Expand All @@ -100,12 +95,15 @@
}

this.text = function() {
var error = this.consumed()
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 Promise.resolve(this.body)
}

return this
Expand Down

0 comments on commit a709976

Please sign in to comment.