acidsound avatar

coffeescript async await 구현

acidsound

Published: 21 Mar 2018 › Updated: 21 Mar 2018coffeescript async await 구현

coffeescript async await 구현

Promise 에서 직렬로 순차대로 실행하는 것은 쉽지 않다.
이런 경우엔 coroutine이 적절한데 async/await으로 할 수 있다.
javascript에 비해 coffeescript 2.0은 await 지원이 깔끔한 편이다.

sleep = (ms) ->
  new Promise (resolve) ->
    setTimeout resolve, ms
say = (text) -> console.log text
countdown = (seconds) ->
  for i in [seconds..1]
    say i
    await sleep 1000
  say "Blast off"

countdown 3

똑같이 ECMA7으로 구현하면

sleep = ms =>
  new Promise(resolve => 
    setTimeout(resolve, ms)
  )
say = text => console.log(text);
countdown = async seconds => {
  for(let i=seconds; i> 0; i--) {
    say(i);
    await sleep(1000);
  }
  say("Blast off");
}
countdown(3);

이와 같다.

병렬로 쏴보내는 건 Promise.all 로 직렬은 Async 를 쓰면 편하다.

Leave coffeescript async await 구현 to:

Written by

function과 reactive를 좋아하는 플루티스트

Read more #kr posts


Best Posts From acidsound

We have not curated any of acidsound's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.

More Posts From acidsound