Mutations doesn't persist immediatly after calling "commit"

Moved from GitHub dgraph-js/39

Posted by Oroneki:

public async confirmaUsuarioViaUuid(uuid: string) {
        const query = `query user($id: string){
            user(func: eq(uuid, $id)) @filter(has(mUser)) {
              uid              
            }
          }`;

        const txn = this._client.newTxn();
        const res = await txn.queryWithVars(query, { $id: uuid });
        const arrUser = res.getJson().user;

        d("arr -> %O", arrUser)
        
        if (arrUser.length === 0) {
            await txn.discard()
            throw new Error("Uuid nao existe.");
        }
        const uid: string = arrUser[0].uid
        try {            
            const data = new Date();
            const p = {
                uid,
                [fieldNames.confirmado]: true,
                [fieldNames.dataAtualizacao]: data,
            };            
            const mu = new dgraph.Mutation();
            mu.setCommitNow(true);
            await mu.setSetJson(p); 
            const assigned = await txn.mutate(mu);
            d("assigned --- ")
            await txn.commit();
            // COMMIT ------------------------------------------
            d("commit --- ")
            assigned.getUidsMap().forEach((id, key) => d(`\n\n\t${key} => ${id}\n\n`));             
            return p;
        } finally {
            await txn.discard();
            d("foi --- ")
        }
    }

I am testing the above method. When I use ratel to see if the changes were persisted I can confirm they were, but my test always show the debug messages "assigned — ", "commit — " and "foi — " after the test been runned. My test is not the problem since I make a call to that method and AFTER the method returns I do another cal to the function below:

public async queryUserByUuid(uuid: string): Promise<any> {
        const query = `query user($id: string){
            user(func: eq(uuid, $id)) @filter(has(mUser)) {
              uid
              uuid
              email
              confirmado
            }
          }`;
        const vars = { $id: uuid };
        const tnx = this._client.newTxn()
        const res = await tnx.queryWithVars(query, vars);
        const ppl = res.getJson();
        await tnx.discard();
        d("query --> %O", ppl.user[0]);
        return ppl.user[0];
    }

The value “confirmado” I receive after calling the second method is still undefined for a moment. If I put a setTimeout on it it returns “true” as expected. I concluded that the “commit” call on the transaction still takes some milisseconds to fully concludes but releases the method before…

Am I missing something or it is really a bug ?

paulftw commented :

Your mutate method is async, so it returns before dgraph callbacks have completed.
If you do something like

public async mutateAndCheck() {
  await confirmaUsuarioViaUuid("some user id");
  await queryUserByUuid("some user id");
}

You should see that the data is read back correctly, even without a setTimeout. This is normal behaviour of JS async methods.

I haven’t tested this code locally, but pretty sure that’s the culprit. Closing the issue for now, if my suggested fix still has a problem - please feel free to reopen and I will take another look.