Back

Why is Promise.all failing in this instance?

  • 0
  • Web
saricden
14 Jun, 2023, 19:11

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:

TypeScript
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.

TL;DR
The user is experiencing an issue where "Promise.all" fails when trying to create documents in the "users" and "profiles" collections. The user suspects that the call to "ID.unique()" needs some delay to generate a unique ID, but this has not been confirmed. When using "Promise.all()", one of the calls to "createDocument" fails almost every time. However, when each promise is awaited individually, the code seems to work consistently. No solution is specified, but a potential fix is suggested by commenting out the "Promise.all()" block.
Binyamin
14 Jun, 2023, 19:20

The ID.unique() just returns a string, the logic is done in the server side. What is the error you're getting?

Side note: Even if you don't use Promise.all the functions will run, the Promise.all is just to make it easy for you to force sync execution of async logic.

saricden
14 Jun, 2023, 19:25

Yeah that's why I'm not really understanding how this is a fix...

There is not error on creation, but the subsequent step (which expects both an entry in the users and profiles collection) will fail with a 500 error

Binyamin
14 Jun, 2023, 19:27

What do you see in the network tab? What are the results from Appwrite

saricden
15 Jun, 2023, 00:32

I'll check next time I'm testing that process, but for now this is a solution, albeit a solution that I don't understand πŸ˜‚

Drake
15 Jun, 2023, 00:42

btw, if you want a promise, you can't put await. so it would need to be:

TypeScript
const promiseUser = db.createDocument();
saricden
15 Jun, 2023, 00:58

Sorry those commented lines in my code were a typo, this is how I had them originally

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more