javascript - 有什么方法可以将 WinJS.Promise 转换为正常的 promise ?

标签 javascript windows-store-apps winjs

我正在 Visual Studio 2017 上制作 Windows 应用商店应用程序,我注意到默认情况下没有 new Promise(),只有 WinJS.Promise,它在某些方面与标准 promise 不同(例如,它没有 .catch,而不是 Promise.all,它有 Promise.join,等等)。

我想知道是否有一种简单的方法可以将 WinJS promise 转换为普通 promise 。

最佳答案

I was wondering if there's an easy way to convert WinJS promises into normal promises.

我已经在 UWP 中测试了正常的 promise,它在我这边有效。

function testPromise() {
    let thisPromiseCount = ++promiseCount;

    let log = document.getElementById('log');
    log.insertAdjacentHTML('beforeend', thisPromiseCount +
        ') Started (<small>Sync code started</small>)<br/>');

    // We make a new promise: we promise a numeric count of this promise, starting from 1 (after waiting 3s)
    let p1 = new Promise(
        // The resolver function is called with the ability to resolve or
        // reject the promise
        (resolve, reject) => {

            log.insertAdjacentHTML('beforeend', thisPromiseCount +
                ') Promise started (<small>Async code started</small>)<br/>');
            // This is only an example to create asynchronism
            window.setTimeout(
                function () {
                    // We fulfill the promise !
                    resolve(thisPromiseCount);
                }, Math.random() * 2000 + 1000);
        }
    );

    // We define what to do when the promise is resolved with the then() call,
    // and what to do when the promise is rejected with the catch() call
    p1.then(
        // Log the fulfillment value
        function (val) {
            log.insertAdjacentHTML('beforeend', val +
                ') Promise fulfilled (<small>Async code terminated</small>)<br/>');
        })
        .catch(
        // Log the rejection reason
        (reason) => {
            console.log('Handle rejected promise (' + reason + ') here.');
        });

    log.insertAdjacentHTML('beforeend', thisPromiseCount +
        ') Promise made (<small>Sync code terminated</small>)<br/>');
}

I'm making windows store apps on Visual Studio 2017 and I notice that by default there is no new Promise(), there is only WinJS.Promise.

我想您已经引用了 WinJS 库,因为涵盖了正常的 promise 。不过理论上,即使你引用了WinJS Libary,也不会影响正常的promise。请尝试在您的环境中运行。并让我知道结果。

关于javascript - 有什么方法可以将 WinJS.Promise 转换为正常的 promise ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47011473/

相关文章:

javascript - Windows 8 应用程序 iframe 尝试访问 Intranet 上的页面内容

javascript - 使用 Google map 标记更改信息窗口中的数据

windows-runtime - 适用于 Windows 应用商店应用程序的 LongListSelector

c++ - CMake VS 不同配置的不同二进制文件

xaml - 将新的 Windows 8.1 搜索框设置为仅使用字形

javascript - WinJS:如何直接在选项中解析全局资源

javascript - 选中复选框 - 为变量分配值并发送提交

javascript - 将 JSON 转换为 Javascript 数组

javascript - 如何让文本动画看起来好像在另一层下面

javascript - 在 Windows 应用商店应用程序安装文件夹内的特定位置创建文件