0

I have if and else statements and one update function that will work based on the completion of the if statement. I want to run a callback to make sure it only run when any of the branches is completed.

if (source !== "itunes"){
    getCustomCover(title);
}
else {
    getiTuensCover(iTunes_data);
}

updateCover(currentCoverURL);

So I want to make sure the updateCover(currentCoverURL); is running when getCustomCover(title); is completed. The same thing for the else branch. Something like $.when() & then().

Thanks in advance.

function getCustomCover(currentTitle){
    $.each(cover_data, function( key, value ) {
        if (value.title === currentTitle){
            currentCoverURL = value.image;
        } else {
            currentCoverURL = settings.default_image;
        }
    });
}
DannyBoy
  • 434
  • 5
  • 21
  • 1
    you need to get familiar with promises https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise – messerbill Aug 11 '19 at 19:39
  • 1
    Is `getCustomCover(title);` running an asynchronous task then? – KeitelDOG Aug 11 '19 at 19:40
  • 1
    If it's async then you need to use Promise directly, or promisify the function getCustomCover, so it will return you a Promise that you can handle with `.then(()=>)` – KeitelDOG Aug 11 '19 at 19:42
  • @KeitelDOG Yes, it goes through a JSON file to find a value. – DannyBoy Aug 11 '19 at 19:42
  • 1
    One last thing. Is the function `getCustomCover(title);` yours? Like you can modify it? Then you wouldn't need a Promise, just a simple Callback Pattern – KeitelDOG Aug 11 '19 at 19:49
  • Yes it is mine. It is basically a `$.each` loop that finds the a string associated to the given `title` than updates a variable `currentCoverURL`. Then I want `updateCover(currentCoverURL)` runs with the updated code. however, since I am keeping the modular pattern as the structure of the code, I would not place `updateCover()` inside the `getCustomCover()` – DannyBoy Aug 11 '19 at 19:54
  • 1
    I updated my answer to adapt to your loop codes. See if it helps – KeitelDOG Aug 11 '19 at 20:13
  • 1
    This question has been answered before for sure, but @Quentin it has nothing to do with an Asynchronous task, the link you pointed for duplicate solution is incorrect. Please change it to a simple Callback Pattern one. – KeitelDOG Aug 11 '19 at 20:21

1 Answers1

0

If this section of the code:

if (source !== "itunes"){
    getCustomCover(title);
}
else {
    getiTuensCover(iTunes_data);
}

is inside the for each, then you might need a promise if you're gonna pass all the result at the same time. But in case your usage of cover can be done async without affection your application, then you might need a Callback Pattern. For example, modify your function to something like this :

var getCustomCover = function (title, callback) {
  var jsonFile = './jsonfile.json';

  fs.readFile(filePath, (err, data) => {
    if (err) {
      callback(err);
    } else {
      var json = JSON.parse(data.toString());
      var cover = json['title'].cover;
      callback(null, cover);
    }
  });
};

Change it according your need. Then call your function :

if (source !== "itunes"){
    getCustomCover(title, (err, coverUrl) => {
        if (err) {
            console.log(err);
        } else {
            updateCover(coverUrl);
        }
    });
}
else {
    // DO THE SAME IF NECCESSARY HERE
    getiTuensCover(iTunes_data);
}

This is an Error-First Callback pattern that is standard in NodeJS.

EDIT --------------

To adapt to the code you just added :

function getCustomCover(currentTitle, callback){
  $.each(cover_data, function( key, value ) {
      if (value.title === currentTitle){
          currentCoverURL = value.image;
      } else {
          currentCoverURL = settings.default_image;
      }

      callback(currentCoverURL);
  });
}

This way the callback function will be called with currentCoverURL arguments as many times as there is covers in the loop.

EDIT 2 -------------

I you're expecting to find only one coverURL inside the loop, then you might consider refactoring you're code like this :

function getCustomCover(currentTitle, callback){
  currentCoverURL = settings.default_image;

  $.each(cover_data, function( key, value ) {
      if (value.title === currentTitle){
          currentCoverURL = value.image;
      }
  });

  callback(currentCoverURL);
}
KeitelDOG
  • 4,750
  • 4
  • 18
  • 33