有什么方法可以将 WinJS.Promise 转换为正常的承诺?
Any way to convert WinJS.Promise into normal promises?
我在 Visual Studio 2017 上制作 windows 商店应用,我注意到默认情况下没有 new Promise()
,只有 WinJS.Promise
,这与标准承诺在某些方面(例如,它没有 .catch
,而不是 Promise.all,它有 Promise.join,等等)。
我想知道是否有一种简单的方法可以将 WinJS 承诺转换为普通承诺。
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 库,因为涵盖了正常的承诺。不过理论上,即使你引用了WinJS Libary,也不会影响正常的promise。请在您的环境中尝试运行。并让我知道结果。
我在 Visual Studio 2017 上制作 windows 商店应用,我注意到默认情况下没有 new Promise()
,只有 WinJS.Promise
,这与标准承诺在某些方面(例如,它没有 .catch
,而不是 Promise.all,它有 Promise.join,等等)。
我想知道是否有一种简单的方法可以将 WinJS 承诺转换为普通承诺。
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 库,因为涵盖了正常的承诺。不过理论上,即使你引用了WinJS Libary,也不会影响正常的promise。请在您的环境中尝试运行。并让我知道结果。