3

I'm writing a react-native app, in which I'm using asyncStorage. Following the recommendations of the docs, I tried using the following wrapper functions:

_storeData = async () => {
  try {
    await AsyncStorage.setItem('@MySuperStore:key', 'I like to save it.');
  } catch (error) {
    // Error saving data
  }
};

And similar ones for retrieving and deleting keys.

This was working fine, but as you can imagine, these functions are used a lot, so having a copy of them in each file is unmanageable. I created a 'utils.js' file, and rewrote the functions like this:

export async function _storeData(key, data) {
  try {
    await AsyncStorage.setItem(key, data);
  } catch (error) {
    console.log("ERROR SAVING");
  }
}

And in the code, instead of using this._storeData(key,data) as before, I used:

import { _storeData } from "../utils";
...
_storeData(key, data)

This does not throw an error, but never seems to actually store, retrieve, or delete anything. I can confirm that when the functions were written as arrow functions within the same file, it worked perfectly.

I'm pretty new to this...

Would it make sense to leave them as arrow functions, and bind them?

Alex
  • 2,270
  • 3
  • 33
  • 65
  • `async` function returns `Promise` so you should either subscribe to the `Promise` with `.then` or wrap in another `async` function with `await` – Karen Grigoryan Feb 24 '19 at 21:23
  • @KarenGrigoryan Thank you. I had already used .then on the retrieveData function (when it was an arrow function), in order to do something to with the retrieved data. I assumed that when storing data, I wouldn't need to actually do anything with the promise, as I didn't need it to return anything, just to store the data – Alex Feb 24 '19 at 21:27
  • is the `_storeData = ` prefixed with `var` or something, or did you accidentally create global variables there? – Bergi Feb 24 '19 at 21:28
  • @Bergi Nope, the code is exactly as I've posted it, so I'm guessing I've made a mistake! – Alex Feb 24 '19 at 21:28
  • I don't know react-native, but where does `AsyncStorage` come from? Is it somehow file-scoped? Otherwise, yes these should work exactly the same. None of the [differences between function declarations and arrow functions](https://stackoverflow.com/q/34361379/1048572) should matter to you. – Bergi Feb 24 '19 at 21:28
  • From this, if it helps: import {AsyncStorage} from 'react-native'; – Alex Feb 24 '19 at 21:30
  • @Alex Ok, what happens if you put a `var` in the arrow function version? – Bergi Feb 24 '19 at 21:31
  • @Alex It might help if you could make a [mcve] of the non-working code, and the same example with working code – Bergi Feb 24 '19 at 21:32
  • I can give it a go... – Alex Feb 24 '19 at 21:34
  • You might try building a small [expo snack](https://snack.expo.io/)...it can help others run/test your react native code – David784 Feb 24 '19 at 21:52
  • You don't need an async function. A simple function would do the job. – 10101010 Feb 26 '19 at 14:47
  • @10101010 Why does the facebook example use async then? – Alex Feb 27 '19 at 00:35

0 Answers0