Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Andrew Elans
Andrew Elans

Posted on • Edited on

     

MSAL: await config load and do the rest

Ref. also to this post of mine:Load scripts in sequence with import(), async and IIFE

When loadingMicrosoft Authentication Library (MSAL) for JavaScript, you have to make sure that the msal config is loaded, the msal object is intitiated andresolved, so that you can use it further in your code:

You must call and await the initialize function before attempting to call any other MSAL API

Setup

msal-config.js

console.log('<<<<<<<<<<<< msal-config.js start');// ...msal configuration data and init here...console.log('<<<<<<<<<<<< msal-config.js end')
Enter fullscreen modeExit fullscreen mode

main.js

console.log('<<<<<<<<<<<< main.js start');// ...msal queries here...console.log('<<<<<<<<<<<< main.js end')
Enter fullscreen modeExit fullscreen mode

Stardard way

Usually you would set it like this in your html:

<scripttype="module"src="/msal-config.js"></script><scripttype="module"src="/main.js"></script>
Enter fullscreen modeExit fullscreen mode

That's what we have as a result (refreshed 3 times):
Image description

We see thatmain.js is called beforemsal-config.js is finished which typicallygives unexpected results in your logic.

Required way

Let's change to the following method:

<script>(async()=>{awaitimport('/msal-config.js');import('/main.js')})();</script>
Enter fullscreen modeExit fullscreen mode

Alternatively with Promise chaining:

<script>import('/msal-config.js').then(()=>import('/main.js'))</script>
Enter fullscreen modeExit fullscreen mode

Result (refreshed 3 times):
Image description

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

This is my sketchbook where I put things I consider relevant to remember in future and share with others.
  • Location
    Oslo, Norway
  • Education
    Teacher of Computer Science 2003 + life-long self-education committment
  • Work
    Digitalizing corporate procurement flows with Microsoft Power Platform and Azure services
  • Joined

More fromAndrew Elans

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp