Javascript 有没有办法把WinJS.Promise转换成普通的承诺?
我在Visual Studio 2017上制作windows应用商店应用程序,我注意到默认情况下没有Javascript 有没有办法把WinJS.Promise转换成普通的承诺?,javascript,windows-store-apps,winjs,Javascript,Windows Store Apps,Winjs,我在Visual Studio 2017上制作windows应用商店应用程序,我注意到默认情况下没有新承诺(),只有WinJS.Promise,它在某些方面与标准承诺不同(例如它没有.catch,而没有Promise.all有Promise.join等等) 我想知道是否有一种简单的方法可以将WinJS承诺转换为普通承诺 我想知道是否有一种简单的方法可以将WinJS承诺转换为普通承诺 我在UWP中测试了正常的promise,它在我这边工作 function testPromise() {
新承诺()
,只有WinJS.Promise
,它在某些方面与标准承诺不同(例如它没有.catch
,而没有Promise.all有Promise.join等等)
我想知道是否有一种简单的方法可以将WinJS承诺转换为普通承诺
我想知道是否有一种简单的方法可以将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/>');
}
函数testPromise(){
让此承诺计数=++承诺计数;
让log=document.getElementById('log');
log.insertAdjacentHTML('beforeend',thisPromiseCount+
“)已启动(同步代码已启动)
”);
//我们做出一个新的承诺:我们承诺从1开始(等待3秒后)对该承诺进行数字计数
让p1=新的承诺(
//调用解析器函数时,可以解析或
//拒绝承诺
(解决、拒绝)=>{
log.insertAdjacentHTML('beforeend',thisPromiseCount+
“)承诺已启动(异步代码已启动)
”);
//这只是创建异步的一个示例
window.setTimeout(
函数(){
//我们履行诺言!
决议(本承诺书);
},Math.random()*2000+1000);
}
);
//我们定义在使用then()调用解析承诺时要做什么,
//当catch()调用拒绝承诺时该怎么办
p1.那么(
//记录实现值
功能(val){
log.insertAdjacentHTML('beforeend',val+
“)承诺实现(异步代码终止)
”;
})
.接住(
//记录拒绝原因
(原因)=>{
log('此处处理拒绝的承诺('+reason+');
});
log.insertAdjacentHTML('beforeend',thisPromiseCount+
“)作出承诺(同步代码终止)
”;
}
我正在Visual Studio 2017上制作windows应用商店应用程序,我注意到默认情况下没有新的Promise(),只有WinJS.Promise
我想你引用了WinJS库,因为正常的承诺被涵盖了。但是,从理论上讲,即使引用WinJS库,也不会影响正常的承诺。请尝试在您的环境中运行。让我知道结果。我用
new Promise()
做了一个新的承诺,没有争论,它失败了,我认为这是因为承诺不存在。我只是没有正确阅读错误消息。如果知道如何将所有WinJS和MobileServiceClient WinJS.promission批量更改为常规承诺,而不必将每一个都包装成普通承诺,那就太好了。