-2

i want to use Async/await in the following code.

function Api(){
  
  fetch('https://api.github.com/users').then(res => res.json())
  .then(data => box.innerHTML=data.map((values) =>`<p>Login:${values.login}</p> 
                                                  <p>ID:${values.id}`).join(''))
}
stofu
  • 110
  • 2
  • 11
  • Make `Api` an `async` function and use `await` instead of `.then()`. See [Making asynchronous programming easier with async and await](https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Asynchronous/Async_await) – Phil Feb 23 '22 at 23:42
  • i tried , but could not do it, should i wrap all it in a variables? – stofu Feb 23 '22 at 23:43
  • Please [edit your question](https://stackoverflow.com/posts/71245312/edit) to show what you tried – Phil Feb 23 '22 at 23:44
  • no...i am fairly new. – stofu Feb 23 '22 at 23:56

1 Answers1

-4

you can try to do it like that

async function Api () {
  let result = await fetch('https://api.github.com/users');
  result = await result.json();
  
  box.innderHTML = result.data.map((values => `<p>Login:${values.login}</p><p>ID:${values.id}`).join(''))
  
  }
Witold
  • 53
  • 2