Typescript 如何重复承诺链中的一步,如何从失败的一步中恢复?
我是JS/TS新手,因此我承诺,我设法将我的“步骤”写为从C#移植的承诺,但我将采用您可以概述的任何构造。(使用ionic2/TS) 为什么承诺: 我在TS中使用Promissions,以便在幻灯片集合中播放幻灯片期间对一系列任务进行排序。(不确定我是否正确地使用承诺,也许还有其他的构造,模式要考虑,请让我知道) 我的目标: 我如何构建承诺链:Typescript 如何重复承诺链中的一步,如何从失败的一步中恢复?,typescript,promise,ionic2,Typescript,Promise,Ionic2,我是JS/TS新手,因此我承诺,我设法将我的“步骤”写为从C#移植的承诺,但我将采用您可以概述的任何构造。(使用ionic2/TS) 为什么承诺: 我在TS中使用Promissions,以便在幻灯片集合中播放幻灯片期间对一系列任务进行排序。(不确定我是否正确地使用承诺,也许还有其他的构造,模式要考虑,请让我知道) 我的目标: 我如何构建承诺链: 我可以迭代一系列步骤 我可以重复步骤2()说5次吗 从步骤1()失败中恢复并继续执行下一个索引/幻灯片(所有后续步骤) 这是我当前的代码大纲 PlayA
PlayAll() {
Step1(Index)
.then(() => {
console.log("PlayAll<<promise------------Step1 DONE");
Step2(i)
.then(() => {
console.log("PlayAll<<promise------------Step2 DONE");
if (Index < Count) {
Index++;
// tell slider to move and wait for its change event
// ???
}
else
// -) Report we are done with all Slides
// Perhaps I should write PlayAll as a promise too?
// instead of using a Callback ??
if (typeof (this.Done) == "function")
this.Done(); // a registered callback.
}
.catch(() => {
console.log("PlayAll<<promise------------Step2 FAIL");
}
.catch(() => {
console.log("PlayAll<<promise------------Step1 FAIL");
}
}
PlayAll(){
步骤1(索引)
.然后(()=>{
log(“播放所有>播放一个媒体是”,媒体);
返回新承诺((解决,拒绝)=>{//返回承诺
var audio=new Howl({src:[Media]});//创建音频wo/src
//为其他方法缓存类级副本,例如暂停
这个.音频=音频;
//
//TODO:需要帮助:
//不知道如何在这里编码拒绝???
//这看起来对吗?
//audio.on('错误',()=>{
//console.log(“音频播放列表>后期播放”);
回报新的承诺(
解析=>{
this.timer1=setTimeout(解析,this.Model.PostPause*1000);
});
}
此链接
说
_从拒绝处理程序返回常规值,会导致调用next.then()resolve处理程序(例如,继续正常处理)_
这就是我通常想要的
我也读过这个链接
但我还是迷路了:)
请尽可能明确
感谢您的帮助。这是我收到的代码。我可以使用while循环重复一系列承诺。从失败的承诺中恢复依赖于正确的拒绝,最初我使用Howler.js播放的声音没有记录事件的正确拼写,并导致本pl中嵌套的承诺出现奇怪的行为回程需要时间 一切都按照我的预期进行了预计TypeScript中可能不支持动态更改while循环限制,因此在当前行为中,似乎一旦输入循环,更改计数直到下一张幻灯片开始才生效,因此发出下一个while循环 我想我能读懂这个承诺。那么,从现在开始抓紧六个月的时间,理解它:) 我不知道TypeScript中支持async/await,我想知道它在这方面有何改进
//
this.PlaySlideRepetitions(Index).then(() => {
console.log("AMSController<<promise------------PlaySlideRepetitions DONE ");
//
if (Index < this.AMSData.Count) {
console.log("AMSController<<------------>> Play Next Slide");
// Tell View to Update
// NOTE: this will cause the view SlideChanged() to call back in here PlayIndex()
this.AMSView_I.SlideNext();
}
else {
console.log("AMSController<<----------->> DONE playing all slides");
this.Done();
}
});
// 20170403
PlaySlideRepetitions(Index: number) {
let DynamicCount = this.GlobalService.AMSRepeatCount_;
let chain = Promise.resolve({}); // trick to kick off this darn Promises :)
var i = 0;
while (i < DynamicCount) {
chain = chain.then(() => {
return this.PlaySlideOnce(Index).then(() => {
console.log("AMSController<<promise------------PlaySlideOnce DONE " + i);
})
.catch(() => {
console.log("AMSController<<promise------------PlaySlideOnce FAIL " + i);
});
});
i++;
DynamicCount = this.GlobalService.AMSRepeatCount_; // may be altered by user
}
return chain;
}
// 20170401
PlaySlideOnce(Index: number) {
console.log("AMSController------------>>PlaySlideOnce Index=", Index);
return new Promise((resolve) => {
// Step 1
this.AudioPlayList.Play(this.AMSData.Audio(Index))
.then(() => {
console.log("AMSController<<promise------------AudioPlayList DONE");
// Step 2
this.SlidePostPause()
.then(() => {
console.log("AMSController<<promise------------SlidePostPause DONE");
resolve();
})
.catch(() => {
console.log("AMSController<<promise------------SlidePostPause FAIL");
});
})
.catch(() => {
console.log("AMSController<<promise------------AudioPlayList FAIL");
// return 123;
// return Promise.resolve(123);
});
});
}
//
这个.playsiler请求(索引)。然后(()=>{
console.log("AMSController这是我收到的代码。我可以使用while循环重复一系列承诺。从失败的承诺中恢复依赖于正确的拒绝,最初我使用Howler.js播放的声音没有记录事件的正确拼写,并导致播放所需的嵌套承诺中出现奇怪的行为
一切都按照我的预期进行了预计TypeScript中可能不支持动态更改while循环限制,因此在当前行为中,似乎一旦输入循环,更改计数直到下一张幻灯片开始才生效,因此发出下一个while循环
我想我能读懂这个承诺。那么,从现在开始抓紧六个月的时间,理解它:)
我不知道TypeScript中支持async/await,我想知道它在这方面有何改进
//
this.PlaySlideRepetitions(Index).then(() => {
console.log("AMSController<<promise------------PlaySlideRepetitions DONE ");
//
if (Index < this.AMSData.Count) {
console.log("AMSController<<------------>> Play Next Slide");
// Tell View to Update
// NOTE: this will cause the view SlideChanged() to call back in here PlayIndex()
this.AMSView_I.SlideNext();
}
else {
console.log("AMSController<<----------->> DONE playing all slides");
this.Done();
}
});
// 20170403
PlaySlideRepetitions(Index: number) {
let DynamicCount = this.GlobalService.AMSRepeatCount_;
let chain = Promise.resolve({}); // trick to kick off this darn Promises :)
var i = 0;
while (i < DynamicCount) {
chain = chain.then(() => {
return this.PlaySlideOnce(Index).then(() => {
console.log("AMSController<<promise------------PlaySlideOnce DONE " + i);
})
.catch(() => {
console.log("AMSController<<promise------------PlaySlideOnce FAIL " + i);
});
});
i++;
DynamicCount = this.GlobalService.AMSRepeatCount_; // may be altered by user
}
return chain;
}
// 20170401
PlaySlideOnce(Index: number) {
console.log("AMSController------------>>PlaySlideOnce Index=", Index);
return new Promise((resolve) => {
// Step 1
this.AudioPlayList.Play(this.AMSData.Audio(Index))
.then(() => {
console.log("AMSController<<promise------------AudioPlayList DONE");
// Step 2
this.SlidePostPause()
.then(() => {
console.log("AMSController<<promise------------SlidePostPause DONE");
resolve();
})
.catch(() => {
console.log("AMSController<<promise------------SlidePostPause FAIL");
});
})
.catch(() => {
console.log("AMSController<<promise------------AudioPlayList FAIL");
// return 123;
// return Promise.resolve(123);
});
});
}
//
这个.playsiler请求(索引)。然后(()=>{
console.log("AMSControllerInterest。也许你需要一个带有状态的自定义类来处理这个问题。强制它进入承诺链会很麻烦。例如,你可以看看这个,看看如何管理状态。这只是为什么要这样做的一个原因。还有很多其他方法:试试异步/等待?这样做会更容易用它书写您的重试/恢复逻辑。@Haocheng您能提供一个您想要的概要吗。谢谢。很有趣。也许您需要一个带有状态的自定义类来处理这个问题。在我看来,将其强制到承诺链中会很混乱。例如,您可以看看如何管理状态。这只是为什么要这样做的一个原因。Ther还有很多其他的方法:tryasync
/await
?用它编写重试/恢复逻辑会更容易。@Haocheng你能提供一个你想要的大纲吗?谢谢。
//
this.PlaySlideRepetitions(Index).then(() => {
console.log("AMSController<<promise------------PlaySlideRepetitions DONE ");
//
if (Index < this.AMSData.Count) {
console.log("AMSController<<------------>> Play Next Slide");
// Tell View to Update
// NOTE: this will cause the view SlideChanged() to call back in here PlayIndex()
this.AMSView_I.SlideNext();
}
else {
console.log("AMSController<<----------->> DONE playing all slides");
this.Done();
}
});
// 20170403
PlaySlideRepetitions(Index: number) {
let DynamicCount = this.GlobalService.AMSRepeatCount_;
let chain = Promise.resolve({}); // trick to kick off this darn Promises :)
var i = 0;
while (i < DynamicCount) {
chain = chain.then(() => {
return this.PlaySlideOnce(Index).then(() => {
console.log("AMSController<<promise------------PlaySlideOnce DONE " + i);
})
.catch(() => {
console.log("AMSController<<promise------------PlaySlideOnce FAIL " + i);
});
});
i++;
DynamicCount = this.GlobalService.AMSRepeatCount_; // may be altered by user
}
return chain;
}
// 20170401
PlaySlideOnce(Index: number) {
console.log("AMSController------------>>PlaySlideOnce Index=", Index);
return new Promise((resolve) => {
// Step 1
this.AudioPlayList.Play(this.AMSData.Audio(Index))
.then(() => {
console.log("AMSController<<promise------------AudioPlayList DONE");
// Step 2
this.SlidePostPause()
.then(() => {
console.log("AMSController<<promise------------SlidePostPause DONE");
resolve();
})
.catch(() => {
console.log("AMSController<<promise------------SlidePostPause FAIL");
});
})
.catch(() => {
console.log("AMSController<<promise------------AudioPlayList FAIL");
// return 123;
// return Promise.resolve(123);
});
});
}