Skip to content

Instantly share code, notes, and snippets.

@bpesquet
Created March 1, 2021 17:41
Show Gist options
  • Save bpesquet/594382b875ef66fa70eaf48aeda544a3 to your computer and use it in GitHub Desktop.
Save bpesquet/594382b875ef66fa70eaf48aeda544a3 to your computer and use it in GitHub Desktop.
Async storage of todos
import AsyncStorage from "@react-native-async-storage/async-storage";
export interface Todo {
task: string;
completed: boolean;
}
class TodoService {
// Return all todos asynchronously. Returns a Promise
async getAll(): Promise<Array<Todo>> {
const keys = await AsyncStorage.getAllKeys();
const jsonTodos = await AsyncStorage.multiGet(keys);
// For all stored todos, parse value (index = 1) into a object
return jsonTodos.map((item) => JSON.parse(item[1]));
}
// ...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment