Reputation: 1467
I am creating a UWP app in Javascript. And I need to translate some code from C#
to javascript.
I am following this Documentation on how to add in-app purchases.
The need code in C#
looks like this:
async void BuyFeature()
{
if (!licenseInformation.ProductLicenses["featureName"].IsActive)
{
try
{
// The customer doesn't own this feature, so
// show the purchase dialog.
await CurrentAppSimulator.RequestProductPurchaseAsync("featureName", false);
//Check the license state to determine if the in-app purchase was successful.
}
catch (Exception)
{
// The in-app purchase was not completed because
// an error occurred.
}
}
else
{
// The customer already owns this feature.
}
}
As you can see in line 9 there is an wait function. How would I translate that into javascript?
So after calling in javascript ...
function abc() {
Windows.ApplicationModel.Store.CurrentAppSimulator.requestProductPurchaseAsync("1", false);
// something fancy here ... but that needs to wait
};
... I need to wait until it returns something and then proceed with the function.
Upvotes: 0
Views: 788
Reputation: 15758
According to your description. I'd suppose your question here is how to use an asynchronous function in UWP with JavaScript.
In many cases, calling an asynchronous function is almost as simple as calling a conventional function. The difference is that you use the then or the done method to assign the handlers for results or errors and to start the operation.
So you can use RequestProductPurchaseAsync in JavaScript like the following. Please note RequestProductPurchaseAsync(System.String,System.Boolean) may be altered or unavailable for releases after Windows 8.1. Instead, use RequestProductPurchaseAsync(System.String).
function abc() {
Windows.ApplicationModel.Store.CurrentAppSimulator.requestProductPurchaseAsync("1").done(
function (purchaseResults) {
//TODO - purchaseResults is the return of requestProductPurchaseAsync method
},
function () {
console.log("error: Unable to buy 1.");
});
}
For more info, please see Asynchronous patterns in UWP using JavaScript and also for how to use CurrentAppSimulator, you may refer to Trial app and in-app purchase sample.
Upvotes: 2
Reputation: 131
I think what you are looking for is using a promise, something like this perhaps:
function productPurchaseAsyncCallExample(){
return {response:'test response',errorCode:'ok/fail etc..'};
// or whatever the response is...
}
var requestProductPurchaseAsync = new Promise(function(resolve, reject){
//We call resolve(...) when what we were doing async succeeded, and reject(...) when it failed.
//In this example, we use setTimeout(...) to simulate async code.
//In reality, you will probabally using something like XHR or an HTML5 API.
//setTimeout(function(){
// resolve({success:'yes', bool:true}); //Yay! Everything went well!
//}, 2250);
resolve(productPurchaseAsyncCallExample());
});
function BuyFeature() {
if (true){ // your if statement check
requestProductPurchaseAsync.then(function(data){
console.log(data);
// do the rest
});
}
};
BuyFeature();
Upvotes: 1
Reputation: 12478
function abc() {
var ans=testFn('a','b'); // let testFn is your function and assume it returns 'txt'
while(ans!="txt"){}
//do rest of code
}
Upvotes: 0