I’m using Promise in my ionic function loadScript, and when I test on browser it’s OK, when I test on some devices android 4.x.x it’s OK, but when I test on my Lenovo K5 Note (Android 5.1),
the Promise doesn’t work and Exactly the debug alerts that my android 5.1 fired are:
alert('0');
alert('1');
alert('inside promise')
alert('return promise');
So it ignores the promise core instructions including:
alert('script loaded');
Below you will find the source code + ionic info
#Home.ts:
//imports + Decorators
export class HomePage {
@ViewChild('map') mapElement: ElementRef;
map: any;
constructor(public navCtrl: NavController,
private platform:Platform,
private geolocation:Geolocation) {
}
ionViewDidLoad(){
alert('0');
this.loadScript()
.then( () => {
alert('initMap1');
this.initMap();
alert('initMap2');
})
.then( () => {
console.log('geolocation');
alert('geolocation')
setTimeout(() => {
this.getGeolocation();
},5000);
alert('geolocation end')
})
.then( () => {
alert('end all');
console.log('end all');
})
}
loadScript(){
alert('1');
var promise = new Promise((resolve, reject) => {
alert("inside promise");
console.log("Async Work Complete");
console.log('start1');
var script = document.createElement('script');
script.src = "https://maps.googleapis.com/maps/api/js?key=AIzaSyBtYmf_L2ESZVcnkeIHRw_uD0VQIBbFYAM&libraries=places";
console.log('append3');
document.head.appendChild(script); //or something of the likes
script.onload = function () {
console.log('resolved');
alert('loaded script');
resolve();
};
});
alert('return promise');
return promise;
}
initMap() {
//Implementation of initMap()
alert('map Loaded');
}
getGeolocation(){
//geoLocation implementation
}
}
}
#ionic info
cli packages: (/usr/lib/node_modules)
@ionic/cli-utils : 1.19.2
ionic (Ionic CLI) : 3.20.0
global packages:
cordova (Cordova CLI) : 7.1.0
local packages:
@ionic/app-scripts : 3.1.8
Cordova Platforms : android 6.3.0
Ionic Framework : ionic-angular 3.9.2
System:
Android SDK Tools : 26.1.1
Node : v8.9.4
npm : 5.7.1
OS : Linux 4.13
Environment Variables:
ANDROID_HOME : xxx
Misc:
backend : pro
cli packages: (/usr/lib/node_modules)
@ionic/cli-utils : 1.19.2
ionic (Ionic CLI) : 3.20.0
global packages:
cordova (Cordova CLI) : 7.1.0
local packages:
@ionic/app-scripts : 3.1.8
Cordova Platforms : android 6.3.0
Ionic Framework : ionic-angular 3.9.2
System:
Android SDK Tools : 26.1.1
Node : v8.9.4
npm : 5.7.1
OS : Linux 4.13
Environment Variables:
ANDROID_HOME : xxx/Sdk
Misc:
backend : pro