简体   繁体   中英

Call back with two parameters ES6 Arrow Function Angular2

Can someone tell me where I am going wrong with my callback function? I need to pass a parameter to the function to check and when it is done checking it sends back true or false.

CheckPlayer(cb,player){
   if(player >0){ 
      console.log("true");
      cb(true);
     }
   else{
      console.log("False");
      cb(false);
      }
}

 //This comes up with many errors
 //The main typescript error says: ',' expected
 CheckMe(){
    player=10;
    isGreat:Boolean;
    this.CheckPlayer((isGreat,player) => {
      if(isGreat)
        console.log("Truth");
      else
        console.log("Fase");
     });
   }

Look at the signature

CheckPlayer(cb,player){

There are two different arguments you need to pass. One is the callback, and the other is the player. You are trying pass only one argument, the callback, and adding the player as a callback parameter. That's not how it works.

Simple pass the callback and the player as arguments to the CheckPlayer , just like you would any other arguments, separated by comma

this.CheckPlayer((isGreat) => {
  if(isGreat)
    console.log("Truth");
  else
    console.log("Fase");
 }, player);

Though usually you have the callback as the last parameter. It just makes for cleaner looking code

CheckPlayer(player, cb){


this.CheckPlayer(player, (isGreat) => {
  if(isGreat)
    console.log("Truth");
  else
    console.log("Fase");
 });

Playground

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM