-
Notifications
You must be signed in to change notification settings - Fork 0
/
promise.js
45 lines (40 loc) · 1.14 KB
/
promise.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
const wordnikAPI = "https://api.wordnik.com/v4/words.json/randomWord?&minLength=5&maxLength=-1&api_key=48dd829661f515d5abc0d03197a00582e888cc7da2484d5c7";
const giphyAPI = "https://api.giphy.com/v1/gifs/search?rating=PG&api_key=dc6zaTOxFJmzC&q=";
//ES6 version
/*
function setup() {
noCanvas();
fetch(wordnikAPI)
.then(response => response.json())
.then(json => {
createP(json.word);
return fetch(giphyAPI + json.word);
})
.then(response => response.json())
.then(json => {
createImg(json.data[0].images['fixed_height_small'].url)
})
.catch(err => console.error(err))
}
*/
//ES8 version
function setup() {
noCanvas();
wordGIF()
.then(results => {
createP(results.word);
createImg(results.img);
})
.catch(err => console.error(err));
}
async function wordGIF() {
let response1 = await fetch(wordnikAPI);
let json1 = await response1.json();
let response2 = await fetch(giphyAPI + json1.word);
let json2 = await response2.json();
let img_url = json2.data[0].images['fixed_height_small'].url
return {
word: json1.word,
img: img_url
}
}