AsyncAwait

async await example

by evgkch

Babel + JSX

const doSomething = (id) => new Promise((resolve, reject) => {
	setTimeout(() => {
  	Math.random() > 0.2
    	? resolve(`${id} done!`)
      : reject(`${id} fall!`)
  }, 1000)
});

const asyncExample = async () => {
	try {
  	for (let i = 0; i < 5; i++) {
    	await doSomething(i).then(result => console.log(result))
    }
  } catch(e) {
  	console.log(e)
  }
}

asyncExample()