Skip to content

Instantly share code, notes, and snippets.

@gzimbron
Last active August 5, 2022 15:14
Show Gist options
  • Save gzimbron/b345a23f0eb3a5698917ea9ee3f60b96 to your computer and use it in GitHub Desktop.
Save gzimbron/b345a23f0eb3a5698917ea9ee3f60b96 to your computer and use it in GitHub Desktop.
Wait for DataStore Load
import { browser } from '$app/env';
import { PubSub } from '@aws-amplify/pubsub';
import { Amplify, DataStore, Hub } from 'aws-amplify';
import awsmobile from 'src/aws-exports';
export default async function startAmplifyConfig() {
if (!browser) return;
Amplify.configure({ ...awsmobile, ssr: true });
PubSub.configure({ ...awsmobile });
DataStore.configure();
await DataStore.clear();
await DataStore.start();
await waitForDataStoreLoad();
}
const waitForDataStoreLoad = async () => {
//promesa que se ejecuta cuando el datastore esta listo
await new Promise<void>((resolve) => {
Hub.listen('datastore', async (hubData) => {
const { event } = hubData.payload;
if (event === 'ready') {
resolve();
}
});
});
};
@mattjwaller
Copy link

Thanks for posting this - solved a massive issue I was having which I thought was a race condition!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment