Camera preview getZoom returns [object Promise]

Hi, Im doing a project using camera preview and would like to call the getZoom function. However, when i run the function it only returns [object Promise]. I tried the setZoom to test my app and the setZoom works fine. Its just the getZoom and the getMaxZoom that would return that object. Help is greatly appreciated. Thanks.

You are supposed to use then to attach code to Promises that runs when they resolve. More information here.

Hi @rapropos , sorry still having difficulties to understand here. My intention was just getting the zoom number from the camera preview. When I run the code CameraPreview.getZoom(), it supposed to return an integer number of the current zoom right? Because I also tried the CameraPreview.setZoom(5), it works fine my camera can zoom. I tried it with the alert("zoom = " + CameraPreview.getZoom()) and it shows me the alert with this message “zoom = [object Promise]”. I was hoping for “zoom = 5”. Please correct me if I have a wrong understanding. Thanks.

No, it returns a Promise that will eventually resolve with that.

That’s never going to happen. If you would like to go down a rabbit hole, there are at least 100 posts by me alone on these forums covering this general topic, so you can take some solace in the fact that you are by no means alone.

CameraPreview.getZoom().then(zoom => {
  // only in here can you do this
// nope, not here

JavaScript has only a single thread of execution, so I think it’s a terrible language to be writing web applications in, but nobody asked me. In order to get around this limitation, futures such as Promises and Observables were invented. Then somebody thought to add even more syntactical sugar that IMHO makes things even more confusing with async and await.

The bottom line is that imperative programming style, like this:

let zoom = CameraPreview.getZoom();

…is going to cause you no end of misery when writing web applications. Life will get much better when you think functionally:

CameraPreview.getZoom().then(zoom => doSomethingWith(zoom));

You don’t know when getZoom will actually return, and you have to train yourself not to care. All you can do is to say “when it does return, here is what I want to do with the result”.

Hi @rapropos, thank you so much for your help. Using “then” really works with the callback function.

About the async and await I couldn’t agree more with you. It makes my life miserable until now. And again thank you so much for your help !!!.