0

currently functionB is called before onDeviceReadyfunctionA but i want to call functionB after the onDeviceReadyfunctionA cordova plugin response.

 document.addEventListener("deviceready", onDeviceReadyfunctionA, false);

 document.addEventListener("DOMContentLoaded", function(event) {functionB();});
technophyle
  • 7,972
  • 6
  • 29
  • 50

1 Answers1

0

Assuming onDeviceReadyFunctionA is an async function/returns a promise you could do:

document.addEventListener("deviceready", onDeviceReadyC, false);

function onDeviceReadyC() {
    onDeviceReadyfunctionA().then(functionB)
}

Or you could do something similar with async/await:

async function onDeviceReady() {
    await onDeviceReadyfunctionA()
    functionB()
  }

sideNote: I think this is the syntax you're looking for with functionB:

document.addEventListener("DOMContentLoaded", functionB);
  function functionB(event) {
    console.log(event)
  }

If you want to use it inline, I'd use an arrow function:

document.addEventListener("DOMContentLoaded", (event) => {console.log(event)});
Mister_CK
  • 668
  • 3
  • 13
  • Thank you so much @Mister_CK. I have one query some pages device ready function return quicker response and some pages DOMContentLoaded inside function B qucier. how to implement the async for DOMContentLoaded and deviceready function – vasanthangel4 Jan 04 '23 at 21:02
  • why do you want to listen to both, are they triggering different things? I don't think you can make DOMContentLoaded and deviceReady themselves asynchronous. But instead the functions that they trigger should be asynchronous. I would only use the onDeviceReady listener and then put then let then call functionA and then Function B from inside the same function as in the first snippet. Or is there a specific reason that you want to use both? – Mister_CK Jan 04 '23 at 21:26
  • yes correct, function A and function B triggering different things. device ready calling the cordova method via functiona A. DOMContentLoad need to trigger for the Appdynamic js(functionB will collect the page title , page url and other information and pass to Appdynamic in DOMContentLoad) – vasanthangel4 Jan 04 '23 at 21:36
  • when i use the device ready also, still cordova plugin response getting null in the DOContentnLoaded. – vasanthangel4 Jan 05 '23 at 12:06
  • android cordova getting called device ready but iOS is giving null – vasanthangel4 Jan 06 '23 at 13:32