promise中写async/await是否合理?

image.png
image.png
handleInitDic方法中的调用的cacheDictionary.getItem('SENDWAY')是别人用async/await写的,因为我现在要用到Promise.all方法,所以要报个promise,不知道我写的对不对?是不是最合理的解决方法?

阅读 2k
3 个回答

不合理,直接return cacheDictionary.getItem('SENDWAY')就可以了,不需要再用Promise包一层

行是行,但你为啥非得用 new Promise 包起来呢?

本质上不就是下面这样么?

handleInitDic() {
    return cacheDictionary
        .getItem('SENDWAY')
        .then((response) => {
            return response.cildren;
        });
}

非要用 async / await 的话,那么这样就好了:

async handleInitDic() {
    const response = await cacheDictionary.getItem('SENDWAY');
    return response.cildren;
}

小建议,问问题时代码不要贴图片

async await就是用来平坦化异步流的所以没有必要在嵌套一层Promise可以把代码修改成如下

async handleInitDic() {
  const response = await cacheDicionary.getItem('SENDWay')
  return response.children 
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题