0

Here is my code to check the image size and width that matches the criteria

im.identify(req.files.image,function (err,features) {      
   //console.log(features);                                
    if(features.width<1000){                               
        console.log('need bigger size');                   
    }                                                      
  });  
 //upload code here
 beginUpload();

Since its asynchronous the beginUpload() function is invoked early so how can i check the image size synchronously. ie i want to upload the image that pass the size criteria

Jabaa
  • 1,743
  • 7
  • 31
  • 60

2 Answers2

1

Call your function in a promise.

When your identify() function is done, the promise (then()) will be execute.

Try this :

im.identify(req.files.image,function (err,features) {      
    //console.log(features);                                
    if(features.width<1000){                   
        console.log('need bigger size');
        //stop the function to don't execute then()
        return false;                 
    }                                                   
}).then(function() {
    //upload code here
    beginUpload();
});
Maitre Manuel
  • 21
  • 1
  • 5
0

@Maitre Manuel correctly notes that the UI can implement a Promise to ensure the asynchronous validation happens before posting.

This SO question discusses some of the code you can use for the check itself. Eg, to determine image width on the client side before posting.

Community
  • 1
  • 1
John Vandivier
  • 2,158
  • 1
  • 17
  • 23