{"size": 1421, "ext": "js", "max_stars_repo_path": "async/serial/index.js", "max_stars_repo_name": "phggg/nodeLesson", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "async/serial/index.js", "max_issues_repo_name": "phggg/nodeLesson", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "async/serial/index.js", "max_forks_repo_name": "phggg/nodeLesson", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 20.3, "max_line_length": 65, "alphanum_fraction": 0.5587614356}
const logTime = (name) => {
  console.log(`Log...${name} ${new Date().toLocaleDateString()}`)
}

exports.callback = () => {
  setTimeout(() => {
    logTime('callback 1')
    setTimeout(() => {
      logTime('callback 2')
    }, 100)
  }, 100)
}

const promise = (name, delay = 100) => new Promise(resolve => {
  setTimeout(() => {
    logTime(name)
    resolve()
  }, delay)
})

exports.promise = () => {
  promise('Promise 1')
    .then(promise('Promise 2'))
    .then(promise('Promise 3'))
}

exports.generator = () => {
  const generator = function* (name) {
    yield promise(name + 1)
    yield promise(name + 2)
    yield promise(name + 3)
  }
  let co = generator => {
    if(it = generator.next().value){
      it.then(res => {
        co(generator)
      })
    }
  }
  co(generator('Co-Generator'))
}

exports.asyncAwait = async () => {
  await promise('Async/Await 1')
  await promise('Async/Await 2')
  await promise('Async/Await 3')
  await promise('Async/Await 4')
}

exports.event = async () => {
  const asyncFun = name => event => {
    setTimeout(() => {
      logTime(name)
      event.emit('end')
    }, 100)
    // return event
  }

  const ary = [
    asyncFun('event 1'),
    asyncFun('event 2'),
    asyncFun('event 3'),
  ]

  const {EventEmitter} = require('events')
  const event = new EventEmitter()
  let i = 0
  event.on('end', () => i < ary.length && ary[i++](event))
  event.emit('end')
}