RE: RE: JavaScript开发笔记
You are viewing a single comment's thread from:

RE: JavaScript开发笔记

Words
0
Reading
0 min
Listen
Play
7M
async function main(){
   await A
   await B
   await C
}
//A,B,C 都是Promise函数,它们将按顺序执行

function timeout(){
  return new Promise(resolve => {
    let f = function(){
      console.log(222, "hello")
      resolve()
    }
    setTimeout(f, 5000)
  })
}

async function main(){
  console.log(111,"hi")
  await timeout()
  console.log(333,"hello")
}
main()

//async函数如果返回值,它会是一个promise,需要使用 .then 获取
async function getReplies(author, permlink, res=[]) {
  let replies = await _this.steem.api.getContentRepliesAsync(author, permlink)
  let children = []
  replies.forEach(item => {
    res.push(item)
    if(item.children > 0){
      //把得到的子数据塞进 .child 中
      children.push(getReplies(item.author, item.permlink, item.child=[]))
    }
  })
  await Promise.all(children)
  return res
}
getReplies(author, permlink)
  .then(res => {
    // console.log(777, res)
    _this.replies= res
  })
@lemooljiang: async function | Ecency