调用协程的方式更有条理吗? [英] More organized way to call Coroutines?

查看:80
本文介绍了调用协程的方式更有条理吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的代码中,上一个请求完成后,我需要调用多个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());.协程名称之前的yieldStartCoroutine应该使它等待该协程返回,然后继续执行其下面的其他代码.

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); yield return www; if(!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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆