Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

upload progress return response.json()? #389

Closed
borm opened this issue Sep 16, 2016 · 7 comments
Closed

upload progress return response.json()? #389

borm opened this issue Sep 16, 2016 · 7 comments

Comments

@borm
Copy link

borm commented Sep 16, 2016

#21

Okay, Fetch API integrated with Streams it`s really cool, but
from https://fetch.spec.whatwg.org/#fetch-api, i correct this sample

function consume(response, total=0) {
  var reader = response.body.getReader()
  return pump()
  function pump() {
    return reader.read().then(({done, value}) => {
      if (done) {
        return response
      }
      total += value.byteLength
      log(`received ${value.byteLength} bytes (${total} bytes in total)`)
      return pump()
    })
  }
}

fetch("/music/pk/altes-kamuffel.flac", {method: 'POST', body: MYFormData})
  .then(response => consume(response))
  .then(response => return response.json() // "Already read" )
  .then((resultFromServer) => log(resultFromServer.file))
  .catch(e => log("something went wrong: " + e))

how return real response.json() from Server?

@annevk
Copy link
Member

annevk commented Sep 16, 2016

You cannot consume a body twice. https://stackoverflow.com/ is a better place for these kind of questions. Hope that helps.

@borm
Copy link
Author

borm commented Sep 16, 2016

You don`t can cache response body from server and save it to other key as example response.result?

Another way, unlock
ReadableStream { locked: true }, and after i can use my sample

fetch("/music/pk/altes-kamuffel.flac", {method: 'POST', body: MYFormData})
  .then(response => return response.json() // "Now i have Json from server" )  
  .then(response => consume(response)) // And show progress
  ...

@annevk
Copy link
Member

annevk commented Sep 17, 2016

I'm not sure what you're saying.

@guest271314
Copy link

guest271314 commented Dec 18, 2016

@annevk It is possible to clone a Response or use .tee(), to consume the "same" body twice, yes?

@annevk
Copy link
Member

annevk commented Dec 19, 2016

Yes.

@borm
Copy link
Author

borm commented Dec 19, 2016

a@guest271314, @annevk its not possible by clone response, after clone, we dont have access to responce.json() on this case we have ReadableStream { locked: true }

@annevk
Copy link
Member

annevk commented Dec 19, 2016

If the first thing you do once you have a response is clone it, that should be possible. If it's not, you might have found a browser issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants