|
|
| (No se muestra una edición intermedia del mismo usuario) |
| Línea 1: |
Línea 1: |
| − | Traer datos con fetch
| + | |
| − | <syntaxhighlight lang="js">
| |
| − | fetch("https://randomuser.me/api/")
| |
| − | .then(function(response) {
| |
| − | // console.log(response)
| |
| − | return response.json()
| |
| − | })
| |
| − | .then(function (Usuario){
| |
| − | console.log('Todo los datos en Array', Usuario)
| |
| − | console.log('Solo el Nombre:', Usuario.results[0].name.first)
| |
| − | })
| |
| − | .catch(function(response) {
| |
| − | })
| |
| − | })</syntaxhighlight>
| |
| − | ==Funciones asincronas==
| |
| − | <syntaxhighlight lang="js">
| |
| − | (async function load(){
| |
| − | // await
| |
| − | const respuesta = await fetch("https://yts.mx/api/v2/list_movies.json?genre=action")
| |
| − | const datos = await respuesta.json()
| |
| − | console.log(datos)
| |
| − | })()
| |
| − | </syntaxhighlight>
| |
| − | Petición de una lista de 50 peliculas de Sci-ficción
| |
| − | <syntaxhighlight lang="js">
| |
| − | (async function load(){
| |
| − | // await
| |
| − | //action, sci-fi, animation, fantasy
| |
| − | async function getDatos(url) {
| |
| − | const respuesta = await fetch(url)
| |
| − | const datos = await respuesta.json()
| |
| − | return datos
| |
| − | }
| |
| − | const scifiList = await getDatos("https://yts.mx/api/v2/list_movies.json?genre=sci-fi&limit=50")
| |
| − | console.log('scifiList', scifiList)
| |
| − | })()
| |
| − | </syntaxhighlight>
| |