-1

I have books object having data stored in file. I am reading data from each file and creating books.

Here issue is createBooks promise then is getting called before completion of reading file and success of createBook method call.

  createBooks: function (books) {
    return new Promise((resolve, reject) => {
        const booksArray = Object.entries(books);
        const aPromises = [];
        booksArray.forEach(([key, value]) => {          
            if(value.data){
                const oFileReader = new FileReader();
                oFileReader.readFile(value.data)
               .then((product) => {
                aPromises.push(this.Service.createBook(books));
               });
            }
        });
        Promise.all(aPromises)
            .then(results => resolve(results))
            .catch(error => reject());
        });
  },
viswa teja
  • 19
  • 2
  • 1
    You're executing `Promise.all` before even pushing the first Promise in the aPromises array. It's executing on an empty array. `readFile` is asynchronous. – Jeremy Thille May 14 '20 at 08:39
  • Does this answer your question? [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Jeremy Thille May 14 '20 at 08:41
  • How I can fix this? – viswa teja May 14 '20 at 08:41
  • 1
    You need to push the *whole promise chain* (i.e. starting from `oFileReader.readFile`) into the array. Also note that using `new Promise` is pointless here, you could `return Promise.all` instead. – jonrsharpe May 14 '20 at 08:41
  • 1
    The FileReader method is called `readAsText()`, and it doesn't return a Promise. Also, you're doing nothing with the returned `product`, instead each Promise is trying to push `this.Service.createBook(books)` into the promise array. –  May 14 '20 at 08:45
  • 1
    Try this version: https://pastebin.com/pepBDHsU –  May 14 '20 at 08:51
  • @ChrisG `booksArray.map(getFileContent).then` isn't going to work – Lennholm May 14 '20 at 09:13
  • @Lennholm True, thanks, code fixed: https://pastebin.com/FpMEEMNT –  May 14 '20 at 09:19
  • there was a typo .Actually it is oFileReader.readFile(value.data) .then((book) => { aPromises.push(this.Service.createBook(book)); }); – viswa teja May 14 '20 at 09:23
  • Your code has more issues than just a typo. Again, there's no `readFile` method, and even if you replace that with an existing one, it still doesn't return a thenable object. –  May 14 '20 at 11:23
  • @ChrisG In my case readFile method is promise which has code for converting to text ``` var oFileReader = new FileReader(); oFileReader.readFile(data) .then(() => { }); – viswa teja May 14 '20 at 11:26
  • Did you add this method to `FileReader.prototype`? –  May 14 '20 at 12:27

1 Answers1

0

Try to log aPromises array values before Promise.all(aPromises). Are there Promise<> objects?

What does the this.Service.createBook(books) return?

FileReader does not have readFile method.

You should use oFileReader.onload handler

oFileReader.onload = function(){
    aPromises.push(this.Service.createBook(books));
};

oFileReader.readAsDataURL(value.data); // value.data - has to be File
// oFileReader.readAsText(value.data); // value.data - has to be blob
vicacid
  • 509
  • 4
  • 6