# Why is code executed in different order in Auth0 Actions?

**URL:** <https://community.auth0.com/t/why-is-code-executed-in-different-order-in-auth0-actions/96570>\
**Category:** Get Help\
**Tags:** actions-management-api, management-api\
**Created:** [December 15, 2022, 7:59pm UTC](https://community.auth0.com/t/why-is-code-executed-in-different-order-in-auth0-actions/96570 "2022-12-15T19:59:04Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![spoudel](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/spoudel/32/51376_2.png) [@spoudel](https://community.auth0.com/u/spoudel)\
**Post date:** [December 15, 2022, 7:59pm UTC](https://community.auth0.com/t/why-is-code-executed-in-different-order-in-auth0-actions/96570/1 "2022-12-15T19:59:05Z")

</div>

Im writing an action to update user metadata which is a phone number field. I get the phone number form the management api which helps me retrieve the mfa phone number. However, due to some issues I am not able to set the user metadata. I think its due to race condition, not sure. I can see the logs when I do a console log, but when I call the set user metadata I get empty string.

 ![Logs in Auth0 Extension](https://global.discourse-cdn.com/auth0/original/3X/8/8/88df49e22d05f3ec63d7e73c80287b7f829a883f.jpeg)

```auto
/**
* Handler that will be called during the execution of a PostLogin flow.
*
* @param {Event} event - Details about the user and the context in which they are logging in.
* @param {PostLoginAPI} api - Interface whose methods can be used to change the behavior of the login.
*/
const ManagementClient = require('auth0').ManagementClient;
exports.onExecutePostLogin = async (event, api) => {
     const AUTH0_MANAGEMENT_API_STATE = new ManagementClient({ domain: event.secrets.domain, clientId: event.secrets.ClientId,clientSecret: event.secrets.ClientSecret,scope: 'read:users update:users'}); //ALl states should subscribe to management api and should have client grant permission and update user and metadata and guardiaan read and delete scope

     console.log("Get User Details Action Started");

	 const namespace = vent.secrets.namespace;
     api.user.setUserMetadata('PhoneNumber', await getPhoneNumber(AUTH0_MANAGEMENT_API_STATE, event.user.user_id));

	 var PhoneNumber= event.user.user_metadata['PhoneNumber'];
   if (event.authorization) {
    // Set claims 
   
    api.idToken.setCustomClaim(`${namespace}/phoneNumber`,PhoneNumber);
   console.log("Logged in Event should be seeen here!!!")
  }
};

async function getPhoneNumber(AUTH0_MANAGEMENT_API_STATE, id, user) 
     {
          await AUTH0_MANAGEMENT_API_STATE.getGuardianEnrollments({ id}, function (err, enrollments) {
               if(err){
                    console.log("There was an error calling get Gaurdian Enrollment");
                    console.log("THis is the error", err);
               }

               console.log("Enrollment from get user detail",enrollments[0]);
               return enrollments[0].phone_number;
          });
     }

/**
* Handler that will be invoked when this action is resuming after an external redirect. If your
* onExecutePostLogin function does not perform a redirect, this function can be safely ignored.
*
* @param {Event} event - Details about the user and the context in which they are logging in.
* @param {PostLoginAPI} api - Interface whose methods can be used to change the behavior of the login.
*/

// exports.onContinuePostLogin = async (event, api) => {
// };

```

---

<div class="post-metadata">

**Author:** ![anish.subedi](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/anish.subedi/32/33385_2.png) [@anish.subedi](https://community.auth0.com/u/anish.subedi)\
**Post date:** [December 20, 2022, 5:05pm UTC](https://community.auth0.com/t/why-is-code-executed-in-different-order-in-auth0-actions/96570/2 "2022-12-20T17:05:45Z")

</div>

Hi spoudel,

Are you using custom database in auth0. Auth0 always trigger get, create and login script asynchronously during the create operation. This need to be happen because it need to check whether user present in external db, and create user in external db and finally triggering login verify the user is being create sucessfully.

---

<div class="post-metadata">

**Author:** ![spoudel](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/spoudel/32/51376_2.png) [@spoudel](https://community.auth0.com/u/spoudel)\
**Post date:** [December 20, 2022, 10:09pm UTC](https://community.auth0.com/t/why-is-code-executed-in-different-order-in-auth0-actions/96570/3 "2022-12-20T22:09:59Z")

</div>

I am not using a custom database. I get its an asynchronous call, but I have set a promise which should set the usermetadata once management client api responds.

---

<div class="post-metadata">

**Author:** ![rueben.tiow](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/rueben.tiow/32/11646_2.png) [@rueben.tiow](https://community.auth0.com/u/rueben.tiow)\
**Post date:** [January 13, 2023, 7:53pm UTC](https://community.auth0.com/t/why-is-code-executed-in-different-order-in-auth0-actions/96570/5 "2023-01-13T19:53:21Z")

</div>

Hi @spoudel,

Thanks for reaching out to the Auth0 Community!

I understand that you have encountered some issues with setting the user’s user\_metadata in a Post Login Action.

> [@spoudel](#):
>
> I get its an asynchronous call, but I have set a promise which should set the usermetadata once management client api responds.

Unfortunately, the Post Login Action executes synchronously as described in our doc [here](https://auth0.com/docs/customize/actions/flows-and-triggers). With that, I recommend using synchronous calls so that there’s an order of execution to ensure that you get the user’s phone number before setting it into the user\_metadata and then setting it as a custom claim.

Please let me know how it goes for you.

Thanks,  
Rueben

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/auth0/original/3X/b/5/b560f266932fcce5d57f09a6595bd50518cae3f3.jpeg) [@system](https://community.auth0.com/u/system)\
**Post date:** [January 27, 2023, 7:53pm UTC](https://community.auth0.com/t/why-is-code-executed-in-different-order-in-auth0-actions/96570/6 "2023-01-27T19:53:43Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
