await is a reserved word 以及將forEach方法的引數改為async函數出現的問題
阿新 • • 發佈:2019-02-10
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); }
再用babel轉碼的時候遇到了await is a reserved word的錯誤,查了資料才知道await命令只能用在async函式之中,而async與await都是對promise的操作
let evaluateTest=async function(arg),
另外一個問題就是起初用的是evaluateArguments.forEach(async function(ele){await evaluateTest(ele)}) 用了大量的console.log()之後發現將forEach方法的引數改成
async函式,evaluateTest(ele)是並行執行也就是同時執行而不是繼發執行。