【发布时间】:2021-01-27 03:24:42
【问题描述】:
正在关注 youtube 上的教程,但不幸的是没有用。有人能告诉我哪里出错了吗?
let posts = [
{name: '1', data: 'Hi1'},
{name: '2', data: 'Hi2'},
]
function getPosts() {
setTimeout(()=> {
posts.forEach((post) => {
console.log(post.name)
})
}, 1000)
}
function createPost(post) {
setTimeout(()=> {
posts.push(post)
}, 2000)
}
async function init() {
await createPost({name: '3', data: 'hey'})
getPosts()
}
init();
【问题讨论】:
-
1.
getPosts和createPost都没有返回承诺,所以await(基本上)在这里没有效果。 2.setTimeout也不返回一个承诺,即使你已经返回了它的值,你也不能await那。 Unless you convert it to a promise
标签: javascript