有什么方法可以将WinJS.Promise转换成正常的承诺?

时间:2017-10-30 09:10:44

标签: javascript windows-store-apps winjs

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

我想知道是否有一种简单的方法可以将WinJS承诺转换为正常的承诺。

1 个答案:

答案 0 :(得分:1)

  

我想知道是否有一种简单的方法可以将WinJS承诺转换为正常的承诺。

我在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/>');
}
  

我在Visual Studio 2017上制作Windows应用程序应用程序,我注意到默认情况下没有新的Promise(),只有WinJS.Promise。

我认为你引用了WinJS Libary导致正常的承诺被覆盖。但是,从理论上讲,即使你引用WinJS Libary,它也不会影响正常的承诺。请尝试在您的环境中运行。让我知道结果。

相关问题