1

Given the below: https://github.com/Uepaa-AG/p2pkit-cordova

I am not figuring out how I'd call onEnabled for example. Not using `ts' the example obviously works great.

But now that I am working with TypeScript, I can't seem to call the nested functions properly.

I have this: cordova.plugins.p2pkit.enable("xyzzy", this.p2pkitCallback, function...

p2pkitCallback() {
        onEnabled() {
          console.log('p2pkit enbled');
          cordova.plugins.p2pkit.enableProximityRanging()
          cordova.plugins.p2pkit.startDiscovery('', cordova.plugins.p2pkit.HIGH_PERFORMANCE) // base64 encoded Data (bytes)=
    }

onEnabled() is highlighted in red of course... How should I refactor the code?

Thanks!

1
  • there's an example on how to do it in the link you provided, ts is the same as js.. the first one actually... the code you pasted is not even valid js/ts. Commented May 22, 2017 at 13:37

1 Answer 1

1

You can pass and call callback functions in Typescript as following;

Declare a callback function argument and call it asynchronous.

public generate(report: Report, callback: () => any = () => {}): void {
    this.generateReport(report, ReportType.PDF).then(callback);
}

Example of passing the callback to the function:

doFilter(payload): void {
    const callback = () => this.filter.hide();
    this.reportService.generate(payload.reportKey, callback);
  }

Note that in my example I optionally assign the callback variable to a noop function to prevent stuff from breaking when no callback is provided.

Sign up to request clarification or add additional context in comments.

1 Comment

I'm pretty sure that if he's going to fix the syntax for the onEnabled property he's going to be fine. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.