While working with Apache Cordova, I encountered a cross-platform challenge related to the Promise
object.
At present, I am initializing a promise in the following manner:
var promise = new Promise(...) {
//Implementation
}
Although this method works fine for most platforms, if the application is operating on Windows, I need to utilize WinJS
instead. This requires me to adjust the code like so:
var promise = new WinJS.Promise(...) {
//Implementation
}
This leads to having redundant code blocks as shown below:
var promise;
if (cordova.platformId == "windows") {
promise = new WinJS.Promise(...) {
//Implementation
}
}
else {
promise = new Promise(...) {
//Same implementation as above
}
}
The primary issue lies in the fact that I am essentially duplicating the implementation within each promise declaration, resulting in identical code segments. This duplication makes it challenging to maintain the code.
Is there a way to instantiate the appropriate Promise
based on the current platform without the need for repeated code segments?