"Conditional" callbacks from asynchronous requests in Javascript

So I've been looking at a bunch of questions on callbacks, and I can't seem to wrap my brain around getting my own code to work properly. I am trying to check URLs with the Soundcloud API to make sure they direct to working sounds.

function urlOK(url){
  SC.initialize({
    client_id: 'my_client_id'
  });
  SC.resolve(url).catch(function(error) {
    console.log(error.message);
    return false; // have tried callback here
  });
  // also want to return true if no errors are found
}

function checkAllInput(){
  if(urlOK(some_url){
    // do more logic
  }
}

With the code shown, urlOK of course blows past the resolve() call and "returns" false in the checkAllInput function. I have tried adding a callback function to urlOK in the spot indicated, and this correctly handled bad URL inputs. Good URL inputs did not "catch" though, and I am terribly confused as to how I should proceed.

I am happy to use jQuery, if it makes anything easier. :) I am also open to totally different approaches, and will answer questions as best as I can. Thank you for any help!

Answers


What you are looking at here is a Promise. not a callback.

See: Promise - MDN

If you wanted to use a callback you would want to do something like this:

function urlOK(url, callback, error){
  SC.initialize({
    client_id: 'my_client_id'
  });

  SC.resolve(url).then(callback).catch(error);
}

function urlIsGood(){
  console.log('Url is good')
}

function urlIsBad(){
  console.log('Url is bad')
}

function checkAllInput(){
  urlOK(some_url, urlIsGood, urlIsBad)
}

Need Your Help

gemfire custom serialization not helping

java serialization deserialization gemfire

I am using gemfire as my cache. The cache heap size is well above 100GB. I discovered that when we put data in gemfire cache from client, it would serialize the data and send to server, and on serv...

Couchdb startkey and endkey does not work when using POST method

couchdb

when using GET method with startkey and endkey, it works fine and gives a list of all "datestamp" that falls in the startkey-endkey range: