1

I am using Ionic with Typescript. I need to access the following api:

load(key, successCallback/*(value)*/, failCallback)

In Typescript I do the following:

cordova.plugins.icloudkv.load('key').then((data) => {
    console.log(data);
    alert('load key: ' + JSON.stringify(data));
      });
    }
  }).catch((e) => {
    console.error(JSON.stringify(e));
    this.doAlert('iCloud: ' + JSON.stringify(e));
  });

However, the alert is never getting fired.

Question

Please can someone advise what's the best way to invoke the javascript function using typescript?

Thanks

4
  • 1
    Well does it return a promise? If not than you probably need to do something like: cordova.plugins.icloudkv.load('key', () =>{}, () =>{}) ?? Commented Jul 13, 2017 at 13:35
  • 1
    You can't treat it like it's a promise if it's not. Either convert it to a promise (or observable - github.com/Reactive-Extensions/RxJS/blob/master/doc/api/core/…), or note that TS is just a subset of JS and call it exactly as you would have before, passing in a callback (arrow) function. Commented Jul 13, 2017 at 13:36
  • You can use methods on your class as success/error handlers. cordova.plugins.icloudkv.load('key', this.someClassMethod, this.someOtherClassMethod); Commented Jul 13, 2017 at 13:36
  • @AlexanderStaroselsky but note that this may behave in unexpected ways if you do so. Commented Jul 13, 2017 at 13:40

3 Answers 3

2

According to the api you've provided your code should look more like:

cordova.plugins.icloudkv.load('key', 
  (data)=>{
    console.log(data);
    alert('load key: ' + JSON.stringify(data));
  }, (e)=>{
    console.error(JSON.stringify(e));
    this.doAlert('iCloud: ' + JSON.stringify(e));
});
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, I am just testing it, and will mark this as the answer if it works
1

Are you getting any errors in the console?

Your code assumes that load returns a Promise. See if this works, since the example explicitly asks for two callbacks in the load method:

cordova.plugins.icloudkv.load('key', (data) => {
        console.log(data);
        alert('load key: ' + JSON.stringify(data));
     }, (e) => {
         console.error(JSON.stringify(e));
         this.doAlert('iCloud: ' + JSON.stringify(e));
 });

2 Comments

Also, bear in mind that with arrow functions this will have a different scope. My example assumes that this is the scope you want.
Thank you. You have the correct answer. But its the same as the answer fro someone who posted just before you, so I'll mark there's as the correct answer. You get an up vote haha
0

If you prefer to work with a Promise even though the api doesn't use one then construct a Promise around the call:

new Promise((resolve, reject) => cordova.plugins.icloudkv.load('key', resolve, reject))
.then((data) => {
    console.log(data);
    alert('load key: ' + JSON.stringify(data));
      });
    }
  }).catch((e) => {
    console.error(JSON.stringify(e));
    this.doAlert('iCloud: ' + JSON.stringify(e));
  });

Comments

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.