Joe
Joe

Reputation: 681

Initializing application before deviceready is fired from Cordova

I have a scenario where I am attempting to initialize my application on the deviceready event from Cordova. Now, this initialization sequence takes a few seconds to execute. This is causing the total launch time to be much larger than desired. My question, is it possible to simple run this initialization sequence in parallel with the Cordova API and plug-ins coming up?

Note: The initialization does not leverage anything from the API or the plug-ins, so could I not simply run it immediately onLoad rather than onDeviceReady?

Upvotes: 2

Views: 568

Answers (1)

SimpleJ
SimpleJ

Reputation: 14768

Assuming your app's initialization uses promises, you can wrap your deviceready listener in a promise and use Promise.all to initialize in parallel with your listener:

const deviceReady = new Promise((resolve) => {
  document.addEventListener("deviceready", resolve, false);
});

function initializeApp() {
  // some async initialization (returns a promise)
}

Promise.all([deviceReady, initializeApp()])
  .then(() => {
    // ready to start
  });

Upvotes: 2

Related Questions