我正在 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/