Coroutine in Cocos Ceator?

Thank you. The great idea :smiley:
I will try!

Wow, i don’t think ts have this thing!
I will try it!
Thank you!

Can i ask a question about : what’s better way to stop/delete a Generator function?

Consume it. Or let your generator itself return at some where.

1赞

I think you are amazing. Can you add a friend?

I’m not :slight_smile:
And of course we’re all friend :))

Can you explain it clearly? :))

还有这种写法?我也是第一次见,刚刚在vscode里试了一下还真的不报错,牛逼,后面具体我再试试。

这种用法不会造成内存泄漏么

不会。。。

持有的 promis 没有终止啊 , 这没有内存泄漏的问题??

我的错,这里 DONT CALLBACK 处应该 reject 一个特定的异常,由上层调用处去进行捕获才行。

comp 中 await函数后面如果 继续处理 this相关对象,但是 这个组件又被销毁了 。那就会报错。
这个有什么通用处理方法能够阻断代码继续执行。

Promise.race

async function(){
await this.fucA();
await this.funcB();
}

等待funcA 执行过程中 comp 销毁了。 怎么让代码不继续执行 funcB;
@jare

export const packCancelableCoroutine = (coroutine:Promise)=>{
let cancel;
let promise = Promise.race([
coroutine,
new Promise((resolve, reject)=>{
cancel = reject;
})
]);
// @ts-ignore
promise.cancel = cancel;
return promise;
};

export default class TestClass extend cc.Component{
private promiseHandle;

onDsetroy(){
    if(this.promiseHandle){
        this.promiseHandle.cancel();
    }
}

async function(){
    try{
        this.promiseHandle = packCancelableCoroutine (this.funA());
        await this.promiseHandle;
        this.promiseHandle = packCancelableCoroutine (this.funB());
        await this.promiseHandle;
    }catch(e){
        //销毁了就直接到这里了
    }

}
}

这个只是取消 异步 ,并没有阻塞代码继续向下执行吧

不是已经有了await this.promiseHandle;吗

:smiley: 你再看看

const {property, ccclass} = cc._decorator;

class Corunner{
    private _gens: Generator[] = [];
    start(co:Generator){
        this._gens.push(co);
    }
    run(){
        for(let i=0; i<this._gens.length; ++i){
            const it = this._gens[i];
            const ans = it.next();
            if(ans.done){
                this._gens.splice(i, 1);
                i--;
            }
        }
    }
}

@ccclass
export default class testGenerator extends cc.Component{

    private _runner: Corunner = null;

    onLoad(){
        this._runner = new Corunner();
        this._runner.start(this.gen());
    }

    *gen(): Generator{
        cc.log('Hello');
        yield;
        cc.log('World');
        yield;
    }

    protected lateUpdate(dt: number): void {
        this._runner.run();
    }
}
1赞