Saturday 29 April 2017

Cloudinary api - resolve promise

I want to write a function that returns a Boolean indicating whether an image with the specified public_id already exists in my Cloudinary space.

I can log the result to the console with the following code:

function isUploaded(public_id) {
  cloudinary.api.resource(public_id, function(response){
    console.log(response.hasOwnProperty('public_id'));
  });
};

isUploaded('test');

However, I want to pass on the result, the Boolean, to another function. Using a return statement results in { state: 'pending' } being logged:

function isUploaded(public_id) {
  return cloudinary.api.resource(public_id, function(response){
    return response.hasOwnProperty('public_id');
  });
};

console.log(isUploaded('test'));

This is has something to do with javascript Promises. I can't seem to restructure my code to make it work though. Any help would be much appreciated.



via The Dancing Code

No comments:

Post a Comment