0

I'm using Ionic Native to read files in the user device, but I need to sort them by modification date.

The method to read the files returns Promise<Entry[]>. To find out the modification date, I need to call Entry's getMetadata method which has the following signature:

getMetadata(successCallback: MetadataCallback, errorCallback?: ErrorCallback): void;

Then in the success callback I have access to the Metadata object which has the modificationDate property I need for sorting.

I'd appreciate any help I can get.

4
  • and you tried what so far? Commented Feb 1, 2018 at 14:50
  • 1
    make all your asyncs, store them somewhere and sort after... Commented Feb 1, 2018 at 14:52
  • Promise.all(asyncCalls.map(() => new Promise((resolve, reject) => getMetadata((...args) => resolve(...args)) might be something like this Commented Feb 1, 2018 at 15:25
  • or just promisify getMetadata Commented Feb 1, 2018 at 15:25

1 Answer 1

1

You'd start by promisifying the getMetadata method:

function getMetadataPromise(entry) {
    return new Promise((resolve, reject) => {
        entry.getMetadata(resolve, reject);
    });
}

Then you can use it in your loop and await all metadata, after which you can sort the array by it:

readEntries().then(entries =>
    Promise.all(entries.map(entry =>
        getMetadataPromise(entry).then(metadata => {
            entry.metadata = metadata;
            return entry;
        })
    ))
).then(entries =>
    entries.sort((a, b) => a.metadata.num - b.metadata.num) // or whatever
).then(sortedEntries => {
    console.log(JSON.stringify(sortedEntries));
}, console.error);
Sign up to request clarification or add additional context in comments.

1 Comment

I had to tweak it a little bit and add some typings but it worked, thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.