async function generate_stock_data() {
await Promise.all([
// api related to low(l) high(h) open(o) current(c)
await fetch(`https://finnhub.io/api/v1/stock/profile2?symbol=AAPL&token=${STOCK_API_KEY}`)
// company information
, await fetch(`https://finnhub.io/api/v1/quote?symbol=AAPL&token=${STOCK_API_KEY}`)
])
.then(async function (responses) {
console.log("here");
// return Promise.all(responses.map(function (response) {
// return response.json();
// }));
let resultJson = [];
for (let i = 0; i < responses.length; i++) {
resultJson[i] = await responses[i].json();
}
return resultJson;
}).then(data => {
console.log(data);
}).catch(error => console.log(error));
}
이걸
async function generate_stock_data() {
try {
const responses = await Promise.all([
// api related to low(l) high(h) open(o) current(c)
fetch(`https://finnhub.io/api/v1/stock/profile2?symbol=AAPL&token=${STOCK_API_KEY}`)
// company information
, fetch(`https://finnhub.io/api/v1/quote?symbol=AAPL&token=${STOCK_API_KEY}`)
])
console.log("here");
// return Promise.all(responses.map(function (response) {
// return response.json();
// }));
let resultJson = [];
for (let i = 0; i < responses.length; i++) {
resultJson[i] = responses[i].json();
}
console.log(resultJson);
} catch (e) {
console.log(e);
}
}
뭐 이런식으로 해서 써라
async/await 해놓고 왜 then/catch랑 썪어서 쓰냐
그리고 await responses[i].json(); 여기서 json으로 파싱하는 곳 비동기 함수 아니지 않음?
시간 써줘서 너무 감사하다... 이렇게 리뷰 해줘서 정말 감사하다.... 후.....
json(); Again this call is asynchronous and returns a promise 맞다... 멍청했다.... 후......
Promise.all 안에서 await 쓸 필요도 없다 Promise.all은 그냥 Promise 리턴하는 함수 실행만 넣으면 된다
Promise.all은 promise를 병렬로 실행시키는 함수인데 너처럼 await 한 결과값을 넣으면 내부적으로 Promise.resolve 시켜서 작동만 되는거지 실제로는 병렬로 실행 안되고 await 순차적으로 실행된다 프로미스 좀 더 공부해라
조언 고맙다... 그렇게 하겠다... 후......