1. ホーム

awaitは予約語であり、forEachメソッドの引数をasync関数に変更した場合の問題点

2022-02-25 14:02:07
 engine.registerHandler('breakpoint:*/EvaluateTest.java:*', async (event, arg1, arg2, detail) => {
            utils.pathEquals(breakpointFile, detail.source.path).should.equal(true);
            detail.line.should.equal(expectedLines[linePos++]);
            console.log('***threads', await engine.threads());
            let evaluateArguments = [{
                type: "const",
                request: "1+2",
                expectedResponse: 3
            }, {
                type: "variable",
                request: "i+1",
                expectedResponse: 2
            }, {
                type: "notExistVariable",
                request: "a",
                expectedResponse: "a cannot be resolved to a variable"
            }, {
                type: "function",
                request: "test()+10",
                expectedResponse: 13
            }];

            let evaluateTest = async arg => {
                let evaluateResponse;
                try {
                    evaluateResponse = await engine.evaluate(arg.request, detail.id, "watch");
                    if (arg.type ! == "notExistVariable") {
                        console.log("******", "Evaluate " + arg.type);
                        assert(evaluateResponse.result.toString() === arg.expectedResponse.toString());
                    }
                }
                catch (ex) {
                    if (arg.type === "notExistVariable") {
                        console.log("******", "Evaluate not exisist variable");
                        assert(ex.message.includes(arg.expectedResponse));
                    }
                }

            };

            for (let ele of evaluateArguments) {
                await evaluateTest(ele);
            }





When using babel to transcode, I encountered the error await is a reserved word, and after checking the documentation, I realized that the await command can only be used in the async function, and both async and await are operations on the promise


 let evaluateTest=async function(arg).
The other problem is that initially we used evaluateArguments.forEach(async function(ele){await evaluateTest(ele)}) and after a lot of console.log() we found that changing the arguments of the forEach method to
The async function, evaluateTest(ele), is executed in parallel, i.e., simultaneously rather than sequentially.

 let evaluateTest=async function(arg).

The other problem is that initially we used evaluateArguments.forEach(async function(ele){await evaluateTest(ele)}) and after a lot of console.log() we found that changing the arguments of the forEach method to

The async function, evaluateTest(ele), is executed in parallel, i.e., simultaneously rather than sequentially.