99 * @providesModule AsyncStorage
1010 * @noflow
1111 * @flow -weak
12+ * @jsdoc
1213 */
1314'use strict' ;
1415
@@ -21,27 +22,58 @@ var RCTAsyncFileStorage = NativeModules.AsyncLocalStorage;
2122var RCTAsyncStorage = RCTAsyncRocksDBStorage || RCTAsyncSQLiteStorage || RCTAsyncFileStorage ;
2223
2324/**
24- * AsyncStorage is a simple, asynchronous, persistent, key-value storage
25+ * @class
26+ * @description
27+ * `AsyncStorage` is a simple, asynchronous, persistent, key-value storage
2528 * system that is global to the app. It should be used instead of LocalStorage.
2629 *
27- * It is recommended that you use an abstraction on top of AsyncStorage instead
28- * of AsyncStorage directly for anything more than light usage since it
29- * operates globally.
30+ * It is recommended that you use an abstraction on top of ` AsyncStorage`
31+ * instead of ` AsyncStorage` directly for anything more than light usage since
32+ * it operates globally.
3033 *
31- * On iOS, AsyncStorage is backed by native code that stores small values in a serialized
32- * dictionary and larger values in separate files. On Android, AsyncStorage will use either
33- * RocksDB or SQLite based on what is available. This JS code is a simple facade that
34- * provides a clear JS API, real Error objects, and simple non-multi functions. Each
35- * method returns a `Promise` object.
34+ * On iOS, `AsyncStorage` is backed by native code that stores small values in a
35+ * serialized dictionary and larger values in separate files. On Android,
36+ * `AsyncStorage` will use either [RocksDB](http://rocksdb.org/) or SQLite
37+ * based on what is available.
38+ *
39+ * The `AsyncStorage` JavaScript code is a simple facade that provides a clear
40+ * JavaScript API, real `Error` objects, and simple non-multi functions. Each
41+ * method in the API returns a `Promise` object.
42+ *
43+ * Persisting data:
44+ * ```
45+ * try {
46+ * await AsyncStorage.setItem('@MySuperStore :key', 'I like to save it.');
47+ * } catch (error) {
48+ * // Error saving data
49+ * }
50+ * ```
51+ *
52+ * Fetching data:
53+ * ```
54+ * try {
55+ * const value = await AsyncStorage.getItem('@MySuperStore :key');
56+ * if (value !== null){
57+ * // We have data!!
58+ * console.log(value);
59+ * }
60+ * } catch (error) {
61+ * // Error retrieving data
62+ * }
63+ * ```
3664 */
3765var AsyncStorage = {
3866 _getRequests : ( [ ] : Array < any > ) ,
3967 _getKeys : ( [ ] : Array < string > ) ,
4068 _immediate : ( null : ?number ) ,
4169
4270 /**
43- * Fetches `key` and passes the result to `callback`, along with an `Error` if
44- * there is any. Returns a `Promise` object.
71+ * Fetches an item for a `key` and invokes a callback upon completion.
72+ * Returns a `Promise` object.
73+ * @param key Key of the item to fetch.
74+ * @param callback Function that will be called with a result if found or
75+ * any error.
76+ * @returns A `Promise` object.
4577 */
4678 getItem : function (
4779 key : string ,
@@ -63,8 +95,12 @@ var AsyncStorage = {
6395 } ,
6496
6597 /**
66- * Sets `value` for `key` and calls `callback` on completion, along with an
67- * `Error` if there is any. Returns a `Promise` object.
98+ * Sets the value for a `key` and invokes a callback upon completion.
99+ * Returns a `Promise` object.
100+ * @param key Key of the item to set.
101+ * @param value Value to set for the `key`.
102+ * @param callback Function that will be called with any error.
103+ * @returns A `Promise` object.
68104 */
69105 setItem : function (
70106 key : string ,
@@ -85,7 +121,11 @@ var AsyncStorage = {
85121 } ,
86122
87123 /**
124+ * Removes an item for a `key` and invokes a callback upon completion.
88125 * Returns a `Promise` object.
126+ * @param key Key of the item to remove.
127+ * @param callback Function that will be called with any error.
128+ * @returns A `Promise` object.
89129 */
90130 removeItem : function (
91131 key : string ,
@@ -105,32 +145,39 @@ var AsyncStorage = {
105145 } ,
106146
107147 /**
108- * Merges existing value with input value, assuming they are stringified json.
109- * Returns a `Promise` object. Not supported by all native implementations .
148+ * Merges an existing `key` value with an input value, assuming both values
149+ * are stringified JSON. Returns a `Promise` object.
110150 *
111- * Example:
112- * ```javascript
151+ * **NOTE:** This is not supported by all native implementations.
152+ *
153+ * @param key Key of the item to modify.
154+ * @param value New value to merge for the `key`.
155+ * @param callback Function that will be called with any error.
156+ * @returns A `Promise` object.
157+ *
158+ * @example <caption>Example</caption>
113159 * let UID123_object = {
114160 * name: 'Chris',
115161 * age: 30,
116162 * traits: {hair: 'brown', eyes: 'brown'},
117163 * };
118-
119- // need only define what will be added or updated
164+ * // You only need to define what will be added or updated
120165 * let UID123_delta = {
121166 * age: 31,
122167 * traits: {eyes: 'blue', shoe_size: 10}
123168 * };
124-
169+ *
125170 * AsyncStorage.setItem('UID123', JSON.stringify(UID123_object), () => {
126171 * AsyncStorage.mergeItem('UID123', JSON.stringify(UID123_delta), () => {
127172 * AsyncStorage.getItem('UID123', (err, result) => {
128173 * console.log(result);
129- * // => {'name':'Chris','age':31,'traits':{'shoe_size':10,'hair':'brown','eyes':'blue'}}
130174 * });
131175 * });
132176 * });
133- * ```
177+ *
178+ * // Console log result:
179+ * // => {'name':'Chris','age':31,'traits':
180+ * // {'shoe_size':10,'hair':'brown','eyes':'blue'}}
134181 */
135182 mergeItem : function (
136183 key : string ,
@@ -151,9 +198,11 @@ var AsyncStorage = {
151198 } ,
152199
153200 /**
154- * Erases *all* AsyncStorage for all clients, libraries, etc. You probably
155- * don't want to call this - use removeItem or multiRemove to clear only your
156- * own keys instead. Returns a `Promise` object.
201+ * Erases *all* `AsyncStorage` for all clients, libraries, etc. You probably
202+ * don't want to call this; use `removeItem` or `multiRemove` to clear only
203+ * your app's keys. Returns a `Promise` object.
204+ * @param callback Function that will be called with any error.
205+ * @returns A `Promise` object.
157206 */
158207 clear : function ( callback ?: ?( error : ?Error ) => void ) : Promise {
159208 return new Promise ( ( resolve , reject ) => {
@@ -169,9 +218,12 @@ var AsyncStorage = {
169218 } ,
170219
171220 /**
172- * Gets *all* keys known to the app, for all callers, libraries, etc. Returns a `Promise` object.
221+ * Gets *all* keys known to your app; for all callers, libraries, etc.
222+ * Returns a `Promise` object.
223+ * @param callback Function that will be called the keys found and any error.
224+ * @returns A `Promise` object.
173225 *
174- * Example: see multiGet for example
226+ * Example: see the ` multiGet` example.
175227 */
176228 getAllKeys : function ( callback ?: ?( error : ?Error , keys : ?Array < string > ) => void ) : Promise {
177229 return new Promise ( ( resolve , reject ) => {
@@ -196,7 +248,7 @@ var AsyncStorage = {
196248 * indicate which key caused the error.
197249 */
198250
199- /** Flushes any pending requests using a single multiget */
251+ /** Flushes any pending requests using a single batch call to get the data. */
200252 flushGetRequests : function ( ) : void {
201253 const getRequests = this . _getRequests ;
202254 const getKeys = this . _getKeys ;
@@ -225,13 +277,23 @@ var AsyncStorage = {
225277 } ,
226278
227279 /**
228- * multiGet invokes callback with an array of key-value pair arrays that
229- * matches the input format of multiSet. Returns a `Promise` object.
280+ * This allows you to batch the fetching of items given an array of `key`
281+ * inputs. Your callback will be invoked with an array of corresponding
282+ * key-value pairs found:
230283 *
231- * multiGet(['k1', 'k2'], cb) -> cb([['k1', 'val1'], ['k2', 'val2']])
284+ * ```
285+ * multiGet(['k1', 'k2'], cb) -> cb([['k1', 'val1'], ['k2', 'val2']])
286+ * ```
287+ *
288+ * The method returns a `Promise` object.
289+ *
290+ * @param keys Array of key for the items to get.
291+ * @param callback Function that will be called with a key-value array of
292+ * the results, plus an array of any key-specific errors found.
293+ * @returns A `Promise` object.
294+ *
295+ * @example <caption>Example</caption>
232296 *
233- * Example:
234- * ```javascript
235297 * AsyncStorage.getAllKeys((err, keys) => {
236298 * AsyncStorage.multiGet(keys, (err, stores) => {
237299 * stores.map((result, i, store) => {
@@ -241,7 +303,6 @@ var AsyncStorage = {
241303 * });
242304 * });
243305 * });
244- * ```
245306 */
246307 multiGet : function (
247308 keys : Array < string > ,
@@ -280,12 +341,20 @@ var AsyncStorage = {
280341 } ,
281342
282343 /**
283- * multiSet and multiMerge take arrays of key-value array pairs that match
284- * the output of multiGet, e.g. Returns a `Promise` object.
344+ * Use this as a batch operation for storing multiple key-value pairs. When
345+ * the operation completes you'll get a single callback with any errors:
285346 *
286- * multiSet([['k1', 'val1'], ['k2', 'val2']], cb);
347+ * ```
348+ * multiSet([['k1', 'val1'], ['k2', 'val2']], cb);
349+ * ```
350+ *
351+ * The method returns a `Promise` object.
287352 *
288- * Example: see multiMerge for an example
353+ * @param keyValuePairs Array of key-value array for the items to set.
354+ * @param callback Function that will be called with an array of any
355+ * key-specific errors found.
356+ * @returns A `Promise` object.
357+ * Example: see the `multiMerge` example.
289358 */
290359 multiSet : function (
291360 keyValuePairs : Array < Array < string >> ,
@@ -305,16 +374,20 @@ var AsyncStorage = {
305374 } ,
306375
307376 /**
308- * Delete all the keys in the `keys` array. Returns a `Promise` object.
377+ * Call this to batch the deletion of all keys in the `keys` array. Returns
378+ * a `Promise` object.
309379 *
310- * Example:
311- * ```javascript
380+ * @param keys Array of key for the items to delete.
381+ * @param callback Function that will be called an array of any key-specific
382+ * errors found.
383+ * @returns A `Promise` object.
384+ *
385+ * @example <caption>Example</caption>
312386 * let keys = ['k1', 'k2'];
313387 * AsyncStorage.multiRemove(keys, (err) => {
314388 * // keys k1 & k2 removed, if they existed
315389 * // do most stuff after removal (if you want)
316390 * });
317- * ```
318391 */
319392 multiRemove : function (
320393 keys : Array < string > ,
@@ -334,56 +407,62 @@ var AsyncStorage = {
334407 } ,
335408
336409 /**
337- * Merges existing values with input values, assuming they are stringified
338- * json. Returns a `Promise` object.
410+ * Batch operation to merge in existing and new values for a given set of
411+ * keys. This assumes that the values are stringified JSON. Returns a
412+ * `Promise` object.
339413 *
340- * Not supported by all native implementations.
414+ * **NOTE**: This is not supported by all native implementations.
341415 *
342- * Example:
343- * ```javascript
344- // first user, initial values
416+ * @param keyValuePairs Array of key-value array for the items to merge.
417+ * @param callback Function that will be called with an array of any
418+ * key-specific errors found.
419+ * @returns A `Promise` object.
420+ *
421+ * @example <caption>Example</caption>
422+ * // first user, initial values
345423 * let UID234_object = {
346424 * name: 'Chris',
347425 * age: 30,
348426 * traits: {hair: 'brown', eyes: 'brown'},
349427 * };
350-
428+ *
351429 * // first user, delta values
352430 * let UID234_delta = {
353431 * age: 31,
354432 * traits: {eyes: 'blue', shoe_size: 10},
355433 * };
356-
434+ *
357435 * // second user, initial values
358436 * let UID345_object = {
359437 * name: 'Marge',
360438 * age: 25,
361439 * traits: {hair: 'blonde', eyes: 'blue'},
362440 * };
363-
441+ *
364442 * // second user, delta values
365443 * let UID345_delta = {
366444 * age: 26,
367445 * traits: {eyes: 'green', shoe_size: 6},
368446 * };
369-
447+ *
370448 * let multi_set_pairs = [['UID234', JSON.stringify(UID234_object)], ['UID345', JSON.stringify(UID345_object)]]
371449 * let multi_merge_pairs = [['UID234', JSON.stringify(UID234_delta)], ['UID345', JSON.stringify(UID345_delta)]]
372-
450+ *
373451 * AsyncStorage.multiSet(multi_set_pairs, (err) => {
374452 * AsyncStorage.multiMerge(multi_merge_pairs, (err) => {
375453 * AsyncStorage.multiGet(['UID234','UID345'], (err, stores) => {
376454 * stores.map( (result, i, store) => {
377455 * let key = store[i][0];
378456 * let val = store[i][1];
379457 * console.log(key, val);
380- * // => UID234 {"name":"Chris","age":31,"traits":{"shoe_size":10,"hair":"brown","eyes":"blue"}}
381- * // => UID345 {"name":"Marge","age":26,"traits":{"shoe_size":6,"hair":"blonde","eyes":"green"}}
382458 * });
383459 * });
384460 * });
385461 * });
386- * ```
462+ *
463+ * // Console log results:
464+ * // => UID234 {"name":"Chris","age":31,"traits":{"shoe_size":10,"hair":"brown","eyes":"blue"}}
465+ * // => UID345 {"name":"Marge","age":26,"traits":{"shoe_size":6,"hair":"blonde","eyes":"green"}}
387466 */
388467 multiMerge : function (
389468 keyValuePairs : Array < Array < string >> ,
0 commit comments