Last active
September 17, 2019 06:21
-
-
Save harrisonmalone/b1a87c0b2c653356a70014374910d738 to your computer and use it in GitHub Desktop.
code from what is a promise lecture
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!DOCTYPE html> | |
| <html lang="en"> | |
| <head> | |
| <meta charset="UTF-8"> | |
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
| <meta http-equiv="X-UA-Compatible" content="ie=edge"> | |
| <title>Document</title> | |
| </head> | |
| <body> | |
| <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script> | |
| <script src="https://unpkg.com/axios/dist/axios.min.js"></script> | |
| <script src="./index.js"></script> | |
| </body> | |
| </html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // console.log(axios) | |
| // const getPokemon = axios.get("https://pokeapi.co/api/v2/pokemon?offset=0&limit=964"); | |
| // // // this is an example of consuming a promise | |
| // getPokemon | |
| // .then((response) => { | |
| // // console.log(moment().format("MMM Do YY")) | |
| // // return response.json() | |
| // console.log(response.data.results) | |
| // }) | |
| // // .then((jsonData) => { | |
| // // const pokemon = jsonData.results | |
| // // pokemon.forEach((poke) => { | |
| // // console.log(poke) | |
| // // }) | |
| // // }) | |
| // .catch((err) => { | |
| // console.log(err.message) | |
| // }) | |
| // client side is in the browser | |
| // server is when we execute javascript using the node keyword on the command line | |
| // setInterval | |
| // const num = 10 | |
| // const myPromise = new Promise((resolve, reject) => { | |
| // setTimeout(() => { | |
| // if (num > 5) { | |
| // resolve('number is greater than 10') | |
| // } else { | |
| // reject('number is not greater than 10') | |
| // } | |
| // }, 0) | |
| // }) | |
| // myPromise | |
| // .then((message) => { | |
| // console.log(message) | |
| // }) | |
| // .catch((failMessage) => { | |
| // console.log(failMessage) | |
| // }) | |
| // pending | |
| // console.log(myPromise) | |
| // console.log('hello') | |
| // console.log('waiting for the promise') | |
| // posting with axios | |
| // const data = { | |
| // name: "morpheus", | |
| // job: "leader" | |
| // } | |
| // const postingPromise = axios.post("https://reqres.in/api/users", { | |
| // "name": "morpheus", | |
| // "job": "leader" | |
| // }) | |
| // const postingPromise = fetch("https://reqres.in/api/users", { | |
| // method: 'POST', | |
| // body: JSON.stringify(data), // data can be `string` or {object}! | |
| // headers: { | |
| // 'Content-Type': 'application/json' | |
| // } | |
| // }) | |
| // postingPromise | |
| // .then((response) => { | |
| // return response.json() | |
| // }) | |
| // .then((data) => { | |
| // console.log(data) | |
| // }) | |
| // .catch((err) => { | |
| // console.log(err.message) | |
| // }) | |
| // .finally(() => { | |
| // console.log('promise is done') | |
| // console.log(postingPromise) | |
| // }) | |
| // const asyncFunc = () => { | |
| // // return new Promise((resolve, reject) => { | |
| // return () => { | |
| // setTimeout(() => { | |
| // return 'hello' | |
| // }, 2000) | |
| // } | |
| // // }) | |
| // } | |
| // const hi = asyncFunc() | |
| // console.log(hi) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment