错误不是指
myfunction而是
start。
async function start() { .... const result = await helper.myfunction('test', 'test');}// My functionconst myfunction = async function(x, y) { return [ x, y, ];}// Start functionconst start = async function(a, b) { const result = await myfunction('test', 'test'); console.log(result);}// Call startstart();我利用这个问题的机会来告诉你一个已知的反模式的使用
await方法:
return await。
错误
async function myfunction() { console.log('Inside of myfunction');}// Here we wait for the myfunction to finish// and then returns a promise that'll be waited for aswell// It's useless to wait the myfunction to finish before to return// we can simply returns a promise that will be resolved later// useless async hereasync function start() { // useless await here return await myfunction();}// Call start(async() => { console.log('before start'); await start(); console.log('after start');})();正确
async function myfunction() { console.log('Inside of myfunction');}// Here we wait for the myfunction to finish// and then returns a promise that'll be waited for aswell// It's useless to wait the myfunction to finish before to return// we can simply returns a promise that will be resolved later// Also point that we don't use async keyword on the function because// we can simply returns the promise returned by myfunctionfunction start() { return myfunction();}// Call start(async() => { console.log('before start'); await start(); console.log('after start');})();另外,要知道有一个
return await正确且重要的特殊情况:(使用try / catch)



