Skip to content

Instantly share code, notes, and snippets.

@Palayoub
Created May 5, 2019 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Palayoub/bdd7573c22b3556b575537d92cb71cd5 to your computer and use it in GitHub Desktop.
Save Palayoub/bdd7573c22b3556b575537d92cb71cd5 to your computer and use it in GitHub Desktop.
fetch(BASE_URL, {
method: "POST",
headers
})
.then(response => response.body)
.then(response => {
const reader = response.getReader();
// read() returns a promise that resolves
// when a value has been received
reader
.read()
.then(function processText({ done, value }) {
// Result objects contain two properties:
// done - true if the stream has already given you all its data.
// value - some data. Always undefined when done is true.
if (done) {
console.log("Stream complete");
return;
}
const formatedValue = JSON.parse(toText.decode(value));
if (formatedValue.responseCode >= 400) {
that.setState({ sent: false, error: formatedValue.data });
}
that.setState({
progress: Number(formatedValue.progress),
progressTitle: formatedValue.title
});
if (that.state.progress === 100) {
that.setState({ data: formatedValue, showProgress: false });
}
// Read some more, and call this function again
return reader.read().then(processText);
})
.catch(err => {
console.log("Error: ", err);
that.setState({ sent: false, error: err });
});
})
.catch(err => {
console.log("Error: ", err);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment