0

I am working on SharePoint 2016 and I have 2 functions I need to run:

SP.SOD.executeFunc('sp.js', 'SP.ClientContext', FirstFunction);
SP.SOD.executeFunc('sp.js', 'SP.ClientContext', SecondFunction);

The SecondFunction should wait for the FirstFunction to run, however they run async. Is there a way to run both sync without using setTimeout() function?

1 Answer 1

0

I've had good results with ExecuteOrDelayUntilScriptLoaded and using async/await functions.

You will need to know what SharePoint JavaScript library you need to wait for, depending on if you need some specific SharePoint functionality loaded or not. In this case sp.runtime.js was all I needed to wait for before my function could be called. It is dependent on SharePoint User information being loaded before it can do its work.

Example:

async function getUserInfo() {
    await firstFunction();
    secondFunction(); // Only need to await the first function to complete in this case.
};

...

ExecuteOrDelayUntilScriptLoaded(getUserInfo, "sp.runtime.js");

You can stage as many functions as you need in the order you want in the getUserInfo function. You can also mix functions you want to await with those that can just run asynchronously .

Updated: In the case of calling the ClientContext the expected behavior is different. In that case you will need to alter your firstFunction.

Example:

function firstFunction(){
    setTimeout(function(){
        var currentContext = new SP.ClientContext.get_current() 
        console.log("1st Func Run",currentContext)
        secondFunction();
    },5000)
}
function secondFunction(){
    var currentContext = new SP.ClientContext.get_current() 
    console.log("2nd func run",currentContext);
}
async function getUserInfo() {
        await firstFunction();
};

ExecuteOrDelayUntilScriptLoaded(getUserInfo, "sp.runtime.js");

Expected result: enter image description here

You might find this helpful as well: Key Points to Know When Using SP.SOD.executeFunc and SP.SOD.executeOrDelayUntilScriptLoaded

5
  • Let me be more clear. Both, FirstFunction and SecondFunction use: "var currentContext = new SP.ClientContext.get_current() and call a OnSuccess and OnFailure functions. The FirstFunction will set a global variable used on the SecondFunction. When I applyed your code I still get an error "Uncaught ReferenceError: Cannot access 'getUserInfo' before initialization at HTMLDocument.anonymous".
    – Seequest
    Feb 14, 2022 at 12:44
  • Sorry, you will need to be sure that the getUserInfo is declared before it is called by ExecuteOrDelayUntilScriptLoaded I will edit my example for clarity. It works for me as my code is compiled by tooling and is moved to the correct location. The other option would be to use a var in place of a const/let as it will be hoisted correctly. Feb 14, 2022 at 14:27
  • Unfortunately, it did not work. The second function is still running before the first one ands.
    – Seequest
    Feb 14, 2022 at 18:15
  • I played around with it a little in SharePoint and see that the Client Context altered the behavior. I was able to get the updated example to work on SharePoint. Visit a SharePoint Page, open the browser console and paste in the example. It will delay for 5 seconds but when it runs the first function will run first and the second after. I hope that helps. Feb 14, 2022 at 18:58
  • That worked... Thank you for your time.
    – Seequest
    Feb 14, 2022 at 19:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.