mirror of
https://github.com/zhigang1992/react-native.git
synced 2026-02-06 17:27:54 +08:00
Add promise support to AsyncStorage
Summary: Since `AsyncStorage` is the primary cache, it would be nice to stick with fetch's promise model and make the common use-case of: 1) check cache 2) make request if cache is invalid more straightforward. Currently if I want to check a cache prior to using fetch (or another promise-based XHR lib) I have to provide a callback. I left the callback support and `resolve`/`reject` the promise after the callback has been applied. Closes https://github.com/facebook/react-native/pull/593 Github Author: Rob McVey <mcvey@thecollective-la.com> Test Plan: Imported from GitHub, without a `Test Plan:` line.
This commit is contained in:
@@ -29,16 +29,17 @@ var COLORS = ['red', 'orange', 'yellow', 'green', 'blue'];
|
||||
|
||||
var BasicStorageExample = React.createClass({
|
||||
componentDidMount() {
|
||||
AsyncStorage.getItem(STORAGE_KEY, (error, value) => {
|
||||
if (error) {
|
||||
this._appendMessage('AsyncStorage error: ' + error.message);
|
||||
} else if (value !== null) {
|
||||
this.setState({selectedValue: value});
|
||||
this._appendMessage('Recovered selection from disk: ' + value);
|
||||
} else {
|
||||
this._appendMessage('Initialized with no selection on disk.');
|
||||
}
|
||||
});
|
||||
AsyncStorage.getItem(STORAGE_KEY)
|
||||
.then((value) => {
|
||||
if (value !== null){
|
||||
this.setState({selectedValue: value});
|
||||
this._appendMessage('Recovered selection from disk: ' + value);
|
||||
} else {
|
||||
this._appendMessage('Initialized with no selection on disk.');
|
||||
}
|
||||
})
|
||||
.catch((error) => this._appendMessage('AsyncStorage error: ' + error.message))
|
||||
.done();
|
||||
},
|
||||
getInitialState() {
|
||||
return {
|
||||
@@ -81,23 +82,17 @@ var BasicStorageExample = React.createClass({
|
||||
|
||||
_onValueChange(selectedValue) {
|
||||
this.setState({selectedValue});
|
||||
AsyncStorage.setItem(STORAGE_KEY, selectedValue, (error) => {
|
||||
if (error) {
|
||||
this._appendMessage('AsyncStorage error: ' + error.message);
|
||||
} else {
|
||||
this._appendMessage('Saved selection to disk: ' + selectedValue);
|
||||
}
|
||||
});
|
||||
AsyncStorage.setItem(STORAGE_KEY, selectedValue)
|
||||
.then(() => this._appendMessage('Saved selection to disk: ' + selectedValue))
|
||||
.catch((error) => this._appendMessage('AsyncStorage error: ' + error.message))
|
||||
.done();
|
||||
},
|
||||
|
||||
_removeStorage() {
|
||||
AsyncStorage.removeItem(STORAGE_KEY, (error) => {
|
||||
if (error) {
|
||||
this._appendMessage('AsyncStorage error: ' + error.message);
|
||||
} else {
|
||||
this._appendMessage('Selection removed from disk.');
|
||||
}
|
||||
});
|
||||
AsyncStorage.removeItem(STORAGE_KEY)
|
||||
.then(() => this._appendMessage('Selection removed from disk.'))
|
||||
.catch((error) => { this._appendMessage('AsyncStorage error: ' + error.message) })
|
||||
.done();
|
||||
},
|
||||
|
||||
_appendMessage(message) {
|
||||
|
||||
Reference in New Issue
Block a user