Massimiliano Comunale
Massimiliano Comunale

Reputation: 51

object Promise Unexpected identifier

I get this error: [object Promise] "Uncaught SyntaxError: Unexpected identifier" on a promise. The code in question is this:

var app = {
init : function() {
    app.start();
},

start : function() {      
    setInterval(app.playerNow().then(data => { app.dataOnAir(data); }), 5000);
},

playerNow : async function() {
   
    let url = "https://www.radiomatese.it/script/app/details.php";
    let response = await fetch(url);
    if(response.ok){
        let data = await response.json();
        return data;
    }
},
dataOnAir : function(data){ .... }
}

the first call works fine it gives me the results but from the second call on it gives me that error

Upvotes: 0

Views: 1208

Answers (1)

Andy
Andy

Reputation: 63579

Your call to playerNow needs to be in a function called by setInterval. It shouldn't be its first argument. Since you're using async/await you can make that async too, and you can simply return response.json() from playerNow - there's no need to assign it to a variable.

const app = {

  init: function() {
    app.start();
  },

  start: function() {
    setInterval(async () => {
      const data = await app.playerNow();
      app.dataOnAir(data);
    }, 5000);
  },

  playerNow: async function() {
    const url = "https://www.radiomatese.it/script/app/details.php";
    const response = await fetch(url);
    if (response.ok) {
      return response.json();
    }
  },
  
  dataOnAir: function(data) {
    console.log(data);
  }

};

app.init();

Upvotes: 2

Related Questions