Created
May 5, 2019 14:21
-
-
Save Palayoub/bdd7573c22b3556b575537d92cb71cd5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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