Skip to content

Instantly share code, notes, and snippets.

@dei-biz
Last active August 20, 2020 09:15
Show Gist options
  • Select an option

  • Save dei-biz/ff636b4a5d44001a3b184396b2f17288 to your computer and use it in GitHub Desktop.

Select an option

Save dei-biz/ff636b4a5d44001a3b184396b2f17288 to your computer and use it in GitHub Desktop.
async function getTodosList() {
const response = await fetch('https://jsonplaceholder.typicode.com/todos'); //fetch es await function. Podemos llamarlo con await y esperará aquí
const data = await response.json(); //.json es método async, podemos llamarlo con await y esperará aquí.
return data; //el return lo hará cuando la línea anterior acabe, después de esperar la conversión a json de la response
}
async function countCompletedTodosTitleLength(){
const todos = await getTodosList(); //Llamamos a getTodosList y esperamos aquí hasta que acabe.
console.log(todos
.filter(todo => todo.completed)
.map(todo => todo.title)
.reduce((acc, title) => acc + title.length, 0)
); //filtramos los completos, sacamos el título y sumamos sus longitudes. Le pasamos eso a console.log
}
countCompletedTodosTitleLength(); //no devuelve nada.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment