Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
350 views
in Technique[技术] by (71.8m points)

javascript - javascript在其外部停止异步函数形式(javascript stop a async function form outside of it)

I'm designing a web application that runs some packages, these packages are a js file which i use eval to run them, they will be executed as a promise function and they can return value with resolving.

(我正在设计一个运行一些程序包的Web应用程序,这些程序包是一个js文件,我使用eval运行它们,它们将作为promise函数执行,并且可以通过解析返回值。)

i store every running package in a variable (lets call it x), now i want to have something like x.abort() that stop the main function execution, i have some tools like scan() that will wait for user input.

(我将每个正在运行的程序包存储在一个变量中(我们称其为x),现在我想使用x.abort()之类的东西来停止主要功能的执行,我需要使用scan()之类的工具来等待用户输入。)

i need to stop function from executing next time when i call x.abort() function.

(我需要在下次调用x.abort()函数时停止执行函数。)

code might help you to understand me better :

(代码可以帮助您更好地了解我:)

// execute function : 
Ash.Vars.Counter += 1;
if (!Ash.Vars.Packages[_package]) {
    console.error(`[Ash] package '${_package}' not found in the database !`);
    return {code: 0, message: `package '${_package}' not found in the database !`};
}
let ai = Ash.Vars.Counter;
let script = eval(Ash.Controllers.Package.GetPackageSource(_package).data);
Ash.Vars.Apps[ai] = new script(_input, _caller, _container, ai);
let main = new Promise(function(_return, _reject) {
    Ash.Vars.Apps[ai].abort = (__outout) => {
        _return(__outout);
    };
    Ash.Vars.Apps[ai].Main()(_return);
});
main.then((_result) => {
    Ash.Vars.Apps[ai].OnTerminate(_result);
});
return main;

and a sample package main.js (this will be executed with function above) :

(和一个示例包main.js(将通过上面的函数执行):)

(() => {
    return class Test extends App {
        Main() {
            return async (_return) => {
                await this.caller.scan('Are you sure 1 : ');
                await this.caller.scan('Are you sure 3 : ');
                await this.caller.scan('Are you sure 4 : ');
                await this.caller.scan('Are you sure 5 : ');
                _return('3');
            }
        }
    }
})();

my current abort function will cause the call of resolve function but function will keep running.

(我当前的中止函数将导致调用resolve函数,但该函数将继续运行。)

i hope you understand my question !

(我希望你能理解我的问题!)

  ask by outlandish translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...