Typescript pass function as parameter


Joachim
  connectWebSocket() {
    const socket = new SockJS('http://localhost:8080/websocket');
    this.stompClient = Stomp.over(socket);

    const _this = this;
    this.stompClient.connect({ "Authorization" : "Bearer "+localStorage.getItem("Authorization")}, function (frame) {
      _this.stompClient.subscribe('/user/queue/notification',function (message) {
        const body = JSON.parse(message.body);
          console.log(body)
      });
    });
  }

This is how I currently connect to the websocket, but I want to achieve something like this

  connectWebSocket(func:Function) {
    const socket = new SockJS('http://localhost:8080/websocket');
    this.stompClient = Stomp.over(socket);

    const _this = this;
    this.stompClient.connect({ "Authorization" : "Bearer "+localStorage.getItem("Authorization")}, function (frame) {
      _this.stompClient.subscribe('/user/queue/notification',func());
    });
  }

and then call it like this

  notificationWS(hello){
    const body = JSON.parse(hello.body);
    if(body.notificationType==="FOLLOW"){
      console.log(body)
    }
  }

this.apiService.connectWebSocket(this.notificationWS.bind(this));

So i want to pass function as parameter to ws function but message is empty

A_A

The problem is, _this.stompClient.subscribe('/user/queue/notification',func());it takes a function as the second parameter. Instead, you have called the function func()and passed the return value.

Try this ( funcinstead func()):

  connectWebSocket(func:Function) {
    const socket = new SockJS('http://localhost:8080/websocket');
    this.stompClient = Stomp.over(socket);

    const _this = this;
    this.stompClient.connect({ "Authorization" : "Bearer "+localStorage.getItem("Authorization")}, function (frame) {
      _this.stompClient.subscribe('/user/queue/notification',func);
    });
  }

Related


typescript - pass type to function parameter

LTFoReal I'm pretty new to Typescript, but I'm trying to pass a custom type to a function. In this case I avoid "any", here is my example. interface ReturnObj<T> { returnObj: T | null } const createResultObj = ( isSuccessful: boolean = false, return

typescript - pass type to function parameter

LTFoReal I'm pretty new to Typescript, but I'm trying to pass a custom type to a function. In this case I avoid "any", here is my example. interface ReturnObj<T> { returnObj: T | null } const createResultObj = ( isSuccessful: boolean = false, return

TypeScript: Pass parameter to callback function

Chaos Zh The following code is from the source code of the azure-accountVSCode extension's code sample . export function activate(context: ExtensionContext) { const azureAccount = extensions.getExtension<AzureAccount>('ms-vscode.azure-account')!.exports;

typescript - pass type to function parameter

LTFoReal I'm pretty new to Typescript, but I'm trying to pass a custom type to a function. In this case I avoid "any", here is my example. interface ReturnObj<T> { returnObj: T | null } const createResultObj = ( isSuccessful: boolean = false, return

typescript - pass type to function parameter

LTFoReal I'm pretty new to Typescript, but I'm trying to pass a custom type to a function. In this case I avoid "any", here is my example. interface ReturnObj<T> { returnObj: T | null } const createResultObj = ( isSuccessful: boolean = false, return

Typescript - pass interface literal to function parameter

Paul Redmond I want to use an interface to set data types and then call them in a function while setting default values without passing the data . I am getting the after canvas error ',' expected.in the function . Can't I call it that? // Options interface opt

How to pass iterable object as function parameter? (typescript)

109 example: function foo(iterable) { for (let i of iterable) {} } What can iterate from here? Other examples include Array.from and most other iterable data structure constructors. 109 Iterable is an ES6 feature, so setting the tsc target to "es6" allows

How to pass iterable object as function parameter? (typescript)

109 example: function foo(iterable) { for (let i of iterable) {} } What can iterate from here? Other examples include Array.from and most other iterable data structure constructors. 109 Iterable is an ES6 feature, so setting the tsc target to "es6" allows

How to pass iterable object as function parameter? (typescript)

109 example: function foo(iterable) { for (let i of iterable) {} } What can iterate from here? Other examples include Array.from and most other iterable data structure constructors. 109 Iterable is an ES6 feature, so setting the tsc target to "es6" allows

Typescript - pass interface literal to function parameter

Paul Redmond I want to use an interface to set data types and then call them in a function while setting default values without passing the data . I am getting the after canvas error ',' expected.in the function . Can't I call it that? // Options interface opt

Typescript - pass interface literal to function parameter

Paul Redmond I want to use an interface to set data types and then call them in a function while setting default values without passing the data . I am getting the after canvas error ',' expected.in the function . Can't I call it that? // Options interface opt

How to pass iterable object as function parameter? (typescript)

109 example: function foo(iterable) { for (let i of iterable) {} } What can iterate from here? Other examples include Array.from and most other iterable data structure constructors. 109 Iterable is an ES6 feature, so setting the tsc target to "es6" allows

TypeScript - Pass anonymous function as method parameter

His Suppose I have implemented a class like List and have a method in it public forEach(func: (id: number, str: string) => void )。 I can call this method like this (where car and _allCars are both instances of this List class) for example this.cars.forEach((ca

Typescript - pass interface literal to function parameter

Paul Redmond I want to use an interface to set data types and then call them in a function while setting default values without passing the data . I am getting the after canvas error ',' expected.in the function . Can't I call it that? // Options interface opt

How to pass iterable object as function parameter? (typescript)

109 example: function foo(iterable) { for (let i of iterable) {} } What can iterate from here? Other examples include Array.from and most other iterable data structure constructors. 109 Iterable is an ES6 feature, so setting the tsc target to "es6" allows

How to pass iterable object as function parameter? (typescript)

109 example: function foo(iterable) { for (let i of iterable) {} } What can iterate from here? Other examples include Array.from and most other iterable data structure constructors. 109 Iterable is an ES6 feature, so setting the tsc target to "es6" allows