Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

61.async/await 小技巧 #66

Open
ccforward opened this issue May 9, 2017 · 4 comments
Open

61.async/await 小技巧 #66

ccforward opened this issue May 9, 2017 · 4 comments

Comments

@ccforward
Copy link
Owner

ccforward commented May 9, 2017

async/await 小技巧

sleep 函数

以前只接使用 setTimeout 和回调函数实现一个 sleep 会有很多的副作用,用起来很不方便。

所以让 setTimeout 搭配使用 async/await

const sleep = delay => {
  return new Promise(resolve => {
    setTimeout(resolve, delay)
  })
}

const fn = async _ => {
  console.log('starting....')
  await sleep(1000)
  console.log('after sleeping for 1 second')
}

搭配 map() 函数

map 中引入异步处理:

const arr = [1,2,3,4,5]
const asyncFn = data => {
  // 异步处理函数
}

const results = arr.map(async num => {
  await asyncFn(num)
  return ++num
})

console.log(results)

代码执行后的结果 [Promise, Promise, Promise, Promise, Promise] 而且 map 函数并不会等异步函数 asyncFn 执行完毕后再返回结果

既然 async 执行后会返回一个 Promise 对象,所以可以通过 Promise.all 的状态来判断异步函数的状态:

const arr = [1,2,3,4,5]
const asyncFn = data => {
  // 异步处理函数
}
const p = arr.map(async num => {
  await asyncFn(num)
  return ++num
})

Promise.all(p).then(results => {
  console.log(results)
})

这样就能正常返回结果 [2, 3, 4, 5, 6]

使用 await 代替 then() 函数

上面的例子最后使用了 Promise.all 还是回到了使用回调函数的方式

这个也很好解决,只需要在外层再加一个 async 函数

const main = async _ => {
  const results = await Promise.all(arr.map(num => {
    await asyncFn()
    return ++num
  }))
  console.log(results)
}

main()

搭配 reduce() 函数

通过引入 async/await 可以把 reduce 扩展成一个按顺序执行异步函数的工具

reduce 用起来很简单:

const arr = [1,2,3,4,5]
const result = arr.reduce((prev, next) => {
  return prev+next
}, 0)

console.log(result)

map 函数一样引入 async/await :

const arr = [1,2,3,4,5]
const main = async _ => {
  const result = await arr.reduce(async (prev, next) => {
    const tmp = await prev
    return tmp + next
  }, Promise.resolve(0))

  console.log(result)
}

main()

而且还可以在 reduce 内部加入异步函数:

const arr = [1,2,3,4,5]
const main = async _ => {
  const result = await arr.reduce(async (prev, next) => {
    const tmp = await prev

    // 异步处理 暂停1s
    await sleep(1000)
    console.log(tmp + next)

    return tmp + next
  }, Promise.resolve(0))
}

main()

上述代码会每隔一秒依次打出 1 3 6 10 15

参考自

http://2ality.com/2016/10/async-function-tips.html

@Thinking80s
Copy link

奇淫巧技啊!

@rccoder
Copy link

rccoder commented May 10, 2017

查了下才知道 reduce 有第二个参数。

async await 用的出神入化啊

@yulonh
Copy link

yulonh commented May 16, 2017

我就问问 arry.replace 怎么用async。。

@justjavac
Copy link

@rccoder 你之前都是怎么使用的啊 😂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants