Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

190
Visualizações
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.

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

The problem is that cloudinary.api.resource runs asynchronously (which is why it requires a callback function).

You can make your isUploaded function return a Promise that resolves once that callback is called.

var cloudinary = require('cloudinary');

function isUploaded(public_id) {
  return new Promise(function (resolve, reject) {
    cloudinary.api.resource(public_id, function(response) {
      var isUploaded = response.hasOwnProperty('public_id');
      resolve(isUploaded);
    });
  });
};

isUploaded('test')
.then(function (result) {
  console.log(result);
})
over 4 years ago · Santiago Trujillo Relatório

0

Note that api.resource() is rate limited (part of the Admin API) so this is not a "scalable" solution.

You can perform a HEAD request and get the statusCode of the response - 200 means the resource exists in your account, 404 otherwise.

For example -

var http = require('http')
var options = {method: 'HEAD', host:'res.cloudinary.com',path:'/<cloud_name>/image/upload/<yourimage.jpg>'} 
var req = http.request(options, function(res) {console.log(res.statusCode);});  
req.end();
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda