Announcement

Collapse
No announcement yet.

How does await and async works in es6?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • How does await and async works in es6?

    How does await and async works in es6?
    • Promises
    Code:
    const delay = seconds => { return new Promise(resolve => { setTimeout(resolve, seconds * 1000) }); }; console.log("Zero seconds wait"); delay(1).then(() => console.log('One seconds wait')); delay(5).then(() => console.log('Five seconds wait'));
    • Loading data with fetch()
    Code:
    const getPeopleInSpace = () => fetch('http://api.open-notify.org/astros.json') .then(res => res.json()); getPeopleInSpace() .then(console.log);
    • Async() and await()
    Code:
    const delay = seconds => { return new Promise( resolve => setTimeout(resolve, seconds * 1000) ) }; const countToFive = async() => { console.log('zero seconds wait'); await delay(2); console.log('Two seconds wait'); await delay(5); console.log('Five seconds wait'); } countToFive();
    • Async with fetch
    Code:
    const githubRequest = async(loginName) => { try{ var response = await fetch(`http://api.github.com/users/${loginName}/followers`); var json = await response.json(); var followerList = json.map(user => user.login); console.log(followerList); } catch(e){ console.log("Data didn't load", e); } }; //githubRequest('eveporcello'); githubRequest('pradeepkumar2');

Related Topics

Collapse

Working...
X