【发布时间】:2018-01-02 19:36:23
【问题描述】:
目前有一个简单的功能:
async function getItem(id) {
const item = await ... doing a bunch of time consuming stuff
return item;
}
来自同步语言,但仍然从表面上理解 async/await;我希望以下内容能够返回已解决的项目:
const item = getItem('items-8');
console.log(item); // Just getting a promise
这个问题有点“装饰性”,但由于 async/await 解决了回调,我希望能走得更远,甚至避免 thenables 的事情。
是否可以使用 async/await 获得这样的单行变量赋值?语法/代码结构是什么?
【问题讨论】:
-
stackoverflow.com/questions/14220321/… 的可能重复项。它涵盖了各种各样的东西,包括 Promise 和 async/await。
-
@estus 如果我们坚持主要答案的标题,我们有
"ES2017+: Promises with async/await"和"ES2015+: Promises with then()"。让我对ES2017部分以"then"结尾的事实感到困惑;然后它应该被宣传为"ES2017+ with still a little bit of ES2015: Promises with async/await/THEN"。这或多或少是我问那里的原因,想知道是否可以使用单线模式
标签: asynchronous async-await es6-promise ecmascript-5