Skip to content

Response typo #40

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

Merged
merged 17 commits into from
Jun 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 51 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -590,10 +590,12 @@ File Access APIs
- [dirs](https://github.com/wkh237/react-native-fetch-blob/wiki/File-System-Access-API#dirs)
- [createFile](https://github.com/wkh237/react-native-fetch-blob/wiki/File-System-Access-API#createfilepath-data-encodingpromise)
- [writeFile (0.6.0)](https://github.com/wkh237/react-native-fetch-blob/wiki/File-System-Access-API#writefilepathstring-contentstring--array-encodingstring-appendbooleanpromise)
- [appendFile (0.6.0) ](https://github.com/wkh237/react-native-fetch-blob/wiki/File-System-Access-API#appendfilepathstring-contentstring--array-encodingstringpromise)
- [appendFile (0.6.0) ](https://github.com/wkh237/react-native-fetch-blob/wiki/File-System-Access-API#appendfilepathstring-contentstring--arraynumber-encodingstring-promisenumber)
- [readFile (0.6.0)](https://github.com/wkh237/react-native-fetch-blob/wiki/File-System-Access-API#readfilepath-encodingpromise)
- [readStream](https://github.com/wkh237/react-native-fetch-blob/wiki/File-System-Access-API#readstreampath-encoding-buffersizepromise)
- [writeStream](https://github.com/wkh237/react-native-fetch-blob/wiki/File-System-Access-API#writestreampathstring-encodingstring-appendbooleanpromise)
- [readStream](https://github.com/wkh237/react-native-fetch-blob/wiki/File-System-Access-API#readstreampath-encoding-buffersize-interval-promisernfbreadstream)
- [hash (0.10.9)](https://github.com/wkh237/react-native-fetch-blob/wiki/File-System-Access-API#hashpath-algorithm-promise)
- [writeStream](https://github.com/wkh237/react-native-fetch-blob/wiki/File-System-Access-API#writestreampathstring-encodingstringpromise)
- [hash](https://github.com/wkh237/react-native-fetch-blob/wiki/File-System-Access-API#hashpath-algorithmpromise)
- [unlink](https://github.com/wkh237/react-native-fetch-blob/wiki/File-System-Access-API#unlinkpathstringpromise)
- [mkdir](https://github.com/wkh237/react-native-fetch-blob/wiki/File-System-Access-API#mkdirpathstringpromise)
- [ls](https://github.com/wkh237/react-native-fetch-blob/wiki/File-System-Access-API#lspathstringpromise)
Expand Down Expand Up @@ -643,6 +645,45 @@ RNFetchBlob.fs.readStream(

When using `writeStream`, the stream object becomes writable, and you can then perform operations like `write` and `close`.

Since version 0.10.9 `write()` resolves with the `RNFetchBlob` instance so you can promise-chain write calls:

```js
RNFetchBlob.fs.writeStream(
PATH_TO_FILE,
// encoding, should be one of `base64`, `utf8`, `ascii`
'utf8',
// should data append to existing content ?
true
)
.then(ofstream => ofstream.write('foo'))
.then(ofstream => ofstream.write('bar'))
.then(ofstream => ofstream.write('foobar'))
.then(ofstream => ofstream.close())
.catch(console.error)
```

or

```js
RNFetchBlob.fs.writeStream(
PATH_TO_FILE,
// encoding, should be one of `base64`, `utf8`, `ascii`
'utf8',
// should data append to existing content ?
true
)
.then(stream => Promise.all([
stream.write('foo'),
stream.write('bar'),
stream.write('foobar')
]))
// Use array destructuring to get the stream object from the first item of the array we get from Promise.all()
.then(([stream]) => stream.close())
.catch(console.error)
```

You should **NOT** do something like this:

```js
RNFetchBlob.fs.writeStream(
PATH_TO_FILE,
Expand All @@ -651,13 +692,18 @@ RNFetchBlob.fs.writeStream(
// should data append to existing content ?
true)
.then((ofstream) => {
// BAD IDEA - Don't do this, those writes are unchecked:
ofstream.write('foo')
ofstream.write('bar')
ofstream.close()
})

.catch(console.error) // Cannot catch any write() errors!
```

The problem with the above code is that the promises from the `ofstream.write()` calls are detached and "Lost".
That means the entire promise chain A) resolves without waiting for the writes to finish and B) any errors caused by them are lost.
That code may _seem_ to work if there are no errors, but those writes are of the type "fire and forget": You start them and then turn away and never know if they really succeeded.

### Cache File Management

When using `fileCache` or `path` options along with `fetch` API, response data will automatically store into the file system. The files will **NOT** removed unless you `unlink` it. There're several ways to remove the files
Expand Down Expand Up @@ -787,4 +833,4 @@ See [release notes](https://github.com/wkh237/react-native-fetch-blob/releases)
### Development

If you're interested in hacking this module, check our [development guide](https://github.com/wkh237/react-native-fetch-blob/wiki/Home), there might be some helpful information.
Please feel free to make a PR or file an issue.
Please feel free to make a PR or file an issue.
4 changes: 2 additions & 2 deletions polyfill/Fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class RNFetchBlobFetchPolyfill {
// release blob cache created when sending request
if(blobCache !== null && blobCache instanceof Blob)
blobCache.close()
return Promise.resolve(new RNFetchBlobFetchRepsonse(resp))
return Promise.resolve(new RNFetchBlobFetchResponse(resp))
})
})

Expand All @@ -97,7 +97,7 @@ class RNFetchBlobFetchPolyfill {

}

class RNFetchBlobFetchRepsonse {
class RNFetchBlobFetchResponse {

constructor(resp:FetchBlobResponse) {
let info = resp.info()
Expand Down