【问题标题】:Javascript: Listen for a callback to returnJavascript:侦听回调以返回
【发布时间】:2017-07-15 06:39:48
【问题描述】:

我正在用 Javascript 创建一个 UWP 应用。我需要将一些代码从 C# 翻译成 javascript。
我正在关注Documentation,了解如何添加应用内购买。

C#中的需求代码如下:

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.
    }
}

正如你在第 9 行看到的,有一个等待函数。我如何将其翻译成 javascript?

所以在调用 javascript 之后...

function abc() {
    Windows.ApplicationModel.Store.CurrentAppSimulator.requestProductPurchaseAsync("1", false);
    // something fancy here ... but that needs to wait
};

...我需要等到它返回一些东西,然后继续执行该函数。

【问题讨论】:

标签: javascript uwp async-await


【解决方案1】:

function abc() {
    var ans=testFn('a','b'); // let testFn is your function and assume it returns 'txt'
    while(ans!="txt"){}
    //do rest of code
    
}

【讨论】:

  • 如果我不知道它返回什么我该怎么办?我只知道它会返回一些东西。
【解决方案2】:

我认为您正在寻找的是使用承诺,可能是这样的:

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();

【讨论】:

  • 在这种情况下我会把Windows.ApplicationModel.Store.CurrentAppSimulator.requestProductPurchaseAsync("1", false);放在哪里?
  • requestProductPurchaseAsync.then 运行 promise 块,因此您的 http 调用(或您正在执行的任何操作)将用 http 调用和参数等替换 setTimeout 所在的位置。然后当您收到响应时调用 resolve( ) 表示返回响应并允许原始 .then 调用继续。
  • 有意义吗?很难用未知数来推测 Windows.ApplicationModel.Store.CurrentAppSimulator.requestPr‌​oductPurchaseAsync("‌​1", false); 究竟做了什么。
  • 我也不知道 :) 我试过这个:Windows.ApplicationModel.Store.CurrentAppSimulator.requestProductPurchaseAsync("1", resolve({success:'yes', bool:true})); 但这会立即解决
  • 试试:resolve(productPurchaseAsyncCallExample()); 你确定你调用的是异步而不是同步吗?
【解决方案3】:

根据您的描述。我想您的问题是如何在 UWP 中通过 JavaScript 使用异步函数。

在许多情况下,调用异步函数几乎与调用常规函数一样简单。不同之处在于您使用thendone 方法为结果或错误分配处理程序并开始操作。

所以你可以在 JavaScript 中使用RequestProductPurchaseAsync,如下所示。请注意RequestProductPurchaseAsync(System.String,System.Boolean) 可能会更改或无法用于 Windows 8.1 之后的版本。请改用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.");
        });
}

更多信息,请参阅Asynchronous patterns in UWP using JavaScript,以及如何使用CurrentAppSimulator,您可以参考Trial app and in-app purchase sample

【讨论】:

    猜你喜欢
    • 2013-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-27
    • 1970-01-01
    相关资源
    最近更新 更多