代码改变世界

Jscex没有xxxAsync().stop()怎么办?

  【当耐特】  阅读(2631)  评论(5编辑  收藏  举报

今天一同事问我Jscex怎么没有stop啊?

异步任务就像断线的风筝,我们没法让它说停就停,但是我们在放飞它之前,可以装个定时炸弹。

 

通常我们可以这样退出一个异步任务:

1
2
3
4
5
6
7
8
var xxxAsync = eval(Jscex.compile("async", function () {
    while (condition) {
        ....
        dosomething
        ....       
        $await(Jscex.Async.sleep(1000));
    }
}))

通过condition判断是否退出异步任务。或者等同于下面这种方式:

1
2
3
4
5
6
7
8
9
var xxxAsync = eval(Jscex.compile("async", function () {
    while (true) {
        if(condition)break;
        ....
        dosomething
        ....       
        $await(Jscex.Async.sleep(1000));
    }
}))

这两种方式的效果是一模一样的。

下面这种方式有个好处就是可以做一些初始化设置什么的:

1
2
3
4
5
6
7
8
9
10
var xxxAsync = eval(Jscex.compile("async", function () {
    while (true) {
        if (condition) {
            //dosomething
            break;
        }
        //dosomething     
        $await(Jscex.Async.sleep(1000));
    }
}))

 

后来,同事又提供了一个要跳出循环的场景

1
2
3
4
5
6
7
8
9
10
11
var xxxAsync = eval(Jscex.compile("async", function () {
    while (true) {
        for (i in XXX) {
            if (condition) {
            //要在这里跳出最外层的循环
            }
        }
        //dosomething     
        $await(Jscex.Async.sleep(1000));
    }
}))

 

所以就只能这样:

1
2
3
4
5
6
7
8
9
10
11
12
13
var xxxAsync = eval(Jscex.compile("async", function () {
    while (true) {
        for (i in XXX) {
            if (condition) {
                //要在这里跳出最外层的循环
                breakTag = true;
            }
        }
        if (breakTag) break;
        //dosomething     
        $await(Jscex.Async.sleep(1000));
    }
}))

后来同事又提供了一个BT的场景:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
var countAsync1 = eval(Jscex.compile("async", function () {
    while (true) {
        for (i in XXX) {
            if (condition) {
                //要在这里跳出最外层的循环
            }
        }
        $await(Jscex.Async.sleep(1000));
    }
}))
var countAsync2 = eval(Jscex.compile("async", function () {
    while (true) {
        for (i in XXX) {
            if (condition) {
                //要在这里跳出最外层的循环
            }
        }    
        $await(Jscex.Async.sleep(1000));
    }
}))
 
var executeAsyncQueue = eval(Jscex.compile("async", function () {
    while (true) {
        $await(countAsync1())
        $await(countAsync2())
        $await(Jscex.Async.sleep(1000));
    }
 
}))
executeAsyncQueue().start();

相当于要在异步队列中跳出最最外层的循环。

我给出的方案还是breakTag

1
2
3
4
5
6
7
8
9
var executeAsyncQueue = eval(Jscex.compile("async", function () {
    while (true) {
        $await(countAsync1())
        $await(countAsync2())
        if (breakTag) break;
        $await(Jscex.Async.sleep(1000));
    }
 
}))

 

同理这个场景也用tag:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
var xxAsync = eval(Jscex.compile("async", function () {
    while (true) {
        //dosomething
        $await(xxxAsync())
        if (breakTag) break;
        $await(Jscex.Async.sleep("1000"));
    }
}))
 
var xxxAsync = eval(Jscex.compile("async", function () {
    if (condition) {
        breakTag = true;
    }
    $await(Jscex.Async.sleep("1000"));
 
}))

所以:Jscex没有必要提供stop,你说呢?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
Jscex.Async.Task = function (delegate) {
    this._delegate = delegate;
    this._handlers = [];
    this.status = "ready";
}
Jscex.Async.Task.prototype = {
    start: function () {
        if (this.status != "ready") {
            throw 'Task can only be started in "ready" status.';
        }
 
        var _this = this;
 
        this.status = "running";
        this._delegate["onStart"](function (type, value) {
 
            if (_this.status != "running") {
                throw ('Callback can only be used in "running" status.');
            }
 
            if (type == "success") {
 
                _this.result = value;
                _this.status = "succeeded";
 
            } else if (type == "failure") {
 
                _this.error = value;
                _this.status = "failed";
 
            } else if (type == "cancel") {
 
                _this.status = "canceled";
 
            } else {
                throw ("Unsupported type: " + type);
            }
             
            _this._notify();
        });
    },
 
    cancel: function () {
        if (this.status != "running") {
            throw 'Task can only be canceled in "running" status';
        }
 
        var onCancel = this._delegate["onCancel"];
        if (onCancel) onCancel();
 
        this._notify();
    },
 
    _notify: function () {
        var handlers = this._handlers;
        delete this._handlers;
         
        for (var i = 0; i < handlers.length; i++) {
            handlers[i](this);
        }
    },
 
    addListener: function (handler) {
        if (!this._handlers) {
            throw ('Listeners can only be added in "ready" or "running" status.');
        }
 
        this._handlers.push(handler);
    }
};
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· SQL Server 2025 AI相关能力初探
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
点击右上角即可分享
微信分享提示