更有条理的调用协程的方式? [英] More organized way to call Coroutines?
问题描述
在我的代码中,我需要在前一个完成后调用多个 Web 请求.例如:
In my code, I have multiple web requests needed to be called after the previous one is finished. For example:
void Init()
{
StartCoroutine(FirstRequest());
}
IEnumerator FirstRequest()
{
www = new WWW(my_url);
yield return www;
StartCoroutine(SecondRequest());
}
IEnumerator SecondRequest()
{
www = new WWW(my_url);
yield return www;
}
如果函数体很大,很容易混淆和凌乱,在Javascript中,有Promise,所以我可以这样做:
If the body of function is huge, it is relly easy to get confusing and messy,in Javascript, there is Promise, so I can do this:
function init() {
return validateParams()
.then(firstRequest)
.then(SecondRequest)
.then((result) => {
console.log(result)
return result
})
}
有人知道我应该如何扩展协程才能产生类似的效果吗?
Any one has a clue how should I extend Coroutines so I can have similar effect?
推荐答案
这很简单.只需使用 yield return SecondRequest();
或 yield return StartCoroutine( SecondRequest());
即可.协程名称或 StartCoroutine
之前的 yield
应该让它等待该协程返回,然后再继续执行它下面的其他代码.
This is very straightforward. Just use yield return SecondRequest();
or yield return StartCoroutine( SecondRequest());
. The yield
before the the coroutine name or StartCoroutine
should make it wait for that coroutine to return before it continue to execute other code below it.
例如,您有四个应按顺序调用的协程函数:
For example, you have four coroutine functions that should be called sequentially:
IEnumerator FirstRequest()
{
www = new WWW(my_url);
yield return www;
}
IEnumerator SecondRequest()
{
www = new WWW(my_url);
yield return www;
}
IEnumerator ThirdRequest()
{
www = new WWW(my_url);
yield return www;
}
IEnumerator FourthRequest()
{
www = new WWW(my_url);
yield return www;
}
然后你可以这样做:
void Init()
{
StartCoroutine(doSequentialStuff());
}
IEnumerator doSequentialStuff()
{
//Do first request then wait for it to return
yield return FirstRequest();
//Do second request then wait for it to return
yield return SecondRequest();
//Do third request then wait for it to return
yield return ThirdRequest();
//Do fourth request then wait for it to return
yield return FourthRequest();
}
<小时>
编辑:
如果我只有成功状态才继续下一个协程怎么办?像 www = new WWW(my_url);收益率返回 www;如果(!www.error)StartCoroutine(SecondRequest());
what if I only proceed to next coroutine if only I got success status? like
www = new WWW(my_url); yield return www; if(!www.error) StartCoroutine(SecondRequest());
在这种情况下,您应该使用 Action
作为协程函数中的参数:
In this case, you should use Action
as a parameter in the coroutine functions:
IEnumerator FirstRequest(Action<bool> reqStat)
{
www = new WWW(my_url);
yield return www;
if (!string.IsNullOrEmpty(www.error))
reqStat(false);
else
reqStat(true);
}
IEnumerator SecondRequest(Action<bool> reqStat)
{
www = new WWW(my_url);
yield return www;
if (!string.IsNullOrEmpty(www.error))
reqStat(false);
else
reqStat(true);
}
IEnumerator ThirdRequest(Action<bool> reqStat)
{
www = new WWW(my_url);
yield return www;
if (!string.IsNullOrEmpty(www.error))
reqStat(false);
else
reqStat(true);
}
IEnumerator FourthRequest(Action<bool> reqStat)
{
www = new WWW(my_url);
yield return www;
if (!string.IsNullOrEmpty(www.error))
reqStat(false);
else
reqStat(true);
}
用法:
void Init()
{
StartCoroutine(doSequentialStuff());
}
IEnumerator doSequentialStuff()
{
bool reqStat = false;
//Do first request then wait for it to return
yield return FirstRequest((status) => { reqStat = status; });
//Do second request then wait for it to return
if (reqStat)
yield return SecondRequest((status) => { reqStat = status; });
//Do third request then wait for it to return
if (reqStat)
yield return ThirdRequest((status) => { reqStat = status; });
//Do fourth request then wait for it to return
if (reqStat)
yield return FourthRequest((status) => { reqStat = status; });
}
这篇关于更有条理的调用协程的方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!