cordova-plugin-file icon indicating copy to clipboard operation
cordova-plugin-file copied to clipboard

Promises

Open GitToTheHub opened this issue 1 year ago • 10 comments
trafficstars

Feature Request

I want to add Promise functionality to this plugin and have some questions.

From my point, Promises are save to add natively, because:

  • For Android the minimum supported Android version is 7.0 since cordova-android 12. Promises are supported since Version 4.4.4
  • For iOS, WKWebView is the required minimum. Promises are supported since iOS 8.

Here a sample of a conversion i would make.

Old code:

FileEntry.prototype.file = function (successCallback, errorCallback) {
    const localURL = this.toInternalURL();
    const win = successCallback && function (f) {
        const file = new File(f.name, localURL, f.type, f.lastModifiedDate, f.size);
        successCallback(file);
    };
    const fail = errorCallback && function (code) {
        errorCallback(new FileError(code));
    };
    exec(win, fail, 'File', 'getFileMetadata', [localURL]);
};

New code:

FileEntry.prototype.file = function (successCallback, errorCallback) {
    return new Promise((resolve, reject) => {
        const localURL = this.toInternalURL();
        const win = function (f) {
            const file = new File(f.name, localURL, f.type, f.lastModifiedDate, f.size);
            if (successCallback) successCallback(file);
            resolve(file);
        };
        const fail = function (code) {
            if (errorCallback) errorCallback(new FileError(code));
            reject(new FileError(code));
        };
        exec(win, fail, 'File', 'getFileMetadata', [localURL]);
    });
};

Would it be fine like this?

Regards

GitToTheHub avatar Sep 29 '24 08:09 GitToTheHub