1
public takePicture() { 
 let options: CaptureImageOptions = { limit: 3 };
    return this.mediaCapture.captureImage(options)
      .then((data: MediaFile[]) => {
        console.log(data);
        for (let i = 0; i <= data.length; i++) {
          return this.beforeSave(data[i].fullPath).then((save) => {
            this.save(save);
          });
        }
      })
      .catch((err: CaptureError) => { console.log(err) });
}

I am trying for each image that I am taking from my camera to phone to call the beforeSave(). However, only for the first one, the promise is returned. How should I implement my code in order to do it for each item in my data array?

3
  • 1
    Do you think Promise.all can help you? developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… Commented Mar 16, 2020 at 15:19
  • @AliF50 can u please give an example? Commented Mar 16, 2020 at 15:24
  • Oh, you're returning right away, okay, let me try. Try removing the return keyword like I have in the answer. Commented Mar 16, 2020 at 15:25

2 Answers 2

1

If the goal here is to save all images that were captured and communicate back completion or error to the caller then you can change to a .map() to collect an array of promises and use Promise.all() to monitor that array of promises. That code looks like this:

public takePicture() { 
    let options: CaptureImageOptions = { limit: 3 };
    return this.mediaCapture.captureImage(options).then((data: MediaFile[]) => {
        console.log(data);
        // collect array of promises and use Promise.all() to monitor them
        return Promise.all(data.map(item => {
            return this.beforeSave(item.fullPath).then((save) => {
              return this.save(save);
            });
        }));
    }).catch((err: CaptureError) => { 
        // log error, then rethrow so caller can see the error
        console.log(err) 
        throw err;
    });
}

FYI, this assumes your use of this.beforeSave() and this.save() is correct. I don't know these interfaces so I'm just taking your lead on this.

Sign up to request clarification or add additional context in comments.

Comments

1

You are returning right away and the for loop does not complete. Try removing the return.

public takePicture() { 
 let options: CaptureImageOptions = { limit: 3 };
    return this.mediaCapture.captureImage(options)
      .then((data: MediaFile[]) => {
        console.log(data);
        for (let i = 0; i < data.length; i++) {
          this.beforeSave(data[i].fullPath).then((save) => { // !! Remove the return here
            this.save(save);
          });
        }
      })
      .catch((err: CaptureError) => { console.log(err) });
}

6 Comments

Also, it should be i < data.length
Answers with zero explanation can be improved by explaining what you changed and why without leaving us to have to do our own visual diff.
A problem with this approach is that it does not handle errors in the this.beforeSave() promise chain or in this.save(). It ignores those errors and could even end with an unhandledRejection. Further, it doesn't propagate errors back to the caller or let the caller know when everything is done. I think your first comment about using Promise.all() is probably what this needs.
@jfriend00 can u suggest a better solution?
@AliF50 this takes time to call the save() why?
|

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.