Rank 2: Process
When I run the below function with using Promise.all() instead of awaiting each promise individually, one of the calls to createDocument will fail almost every time.
Whereas, when I run the code as is, it seems to work every time.
Does anyone have any idea why?
Code:
export async function register(username: string, avatarFile: any) { try { const accountData = await account.get(); const {$id: auth_id} = accountData; let avatar_id = null;
if (avatarFile) { const file = await storage.createFile('profile_pictures', ID.unique(), avatarFile); avatar_id = file.$id; }
// const promiseUser = await db.createDocument( await db.createDocument( 'chat', 'users', ID.unique(), { auth_id } );
// const promiseProfile = await db.createDocument( await db.createDocument( 'chat', 'profiles', ID.unique(), { auth_id, username, avatar_id } );
// Not sure why, but this was causing either the users or the profiles doc creation fail, forcing user to register twice // await Promise.all([ // promiseUser, // promiseProfile // ]); } catch (e) { console.warn(e); }}The only thing I can think of is perhaps ID.unique() needs a few milliseconds to get a new unique ID? But even then I'm not sure that would cause one of the writes to fail.
I've ran about 6 tests against the working method though, and it seems to work consistently.