ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

๊ธฐ์กด ์ฝ”๋“œ

$searchInput.addEventListener('keyup', async function(event) {
    $loadingIndicator.style.visibility = 'visible'; //loading UI ์›น ์ƒ์— ๋žœ๋”๋ง
    await searchKeyword(event.target.value, searchKeywordApi, showKeyword);
})
const searchKeywordApi = (data) => {
    let res = fetch(`${API_URL}?q=${data}`).then(data => {
        return data.json();
    })
    return res;
}

const searchKeyword = async (data, callApi, callback) => {
    try {
        let res = await callApi(data); //์š”์ฒญ ์ฒ˜๋ฆฌ ํ›„ ๊ฒฐ๊ณผ ๋ฆฌํ„ด๋  ๋•Œ๊นŒ์ง€ ๊ธฐ๋‹ค๋ฆผ(์ง€์—ฐ)
        $loadingIndicator.style.visibility = 'hidden'; //loading UI ์›น ์ƒ์—์„œ ๋ณด์ด์ง€ ์•Š๊ฒŒ ์ฒ˜๋ฆฌ
        
        (...)
        
    } catch(error) {
        console.log(error);
    }
}

Input ์š”์†Œ์—์„œ keyup ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด API ์š”์ฒญ ์ฒ˜๋ฆฌ ๋™์•ˆ ์‚ฌ์šฉ์ž์—๊ฒŒ ์ฒ˜๋ฆฌ ์ค‘์ด๋ผ๋Š” ์‚ฌ์‹ค์„ ์‹œ๊ฐ์ ์œผ๋กœ ๋ณด์—ฌ์ฃผ๊ธฐ ์œ„ํ•ด ๋‹ค์Œ๊ณผ ๊ฐ™์ด

loading ๊ด€๋ จ UI๋ฅผ ๋ณด์—ฌ์ฃผ๋„๋ก ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜์˜€๋‹ค.

 

The await expression causes async function execution to pause until a Promise is settled (that is, fulfilled or rejected), and to resume execution of the async function after fulfillment. When resumed, the value of the await expression is that of the fulfilled Promise.

 

ํ•˜์ง€๋งŒ ์ด๋ ‡๊ฒŒ ๋™๊ธฐ ์ฒ˜๋ฆฌ๋กœ ์ˆ˜ํ–‰๋  ์ฝ”๋“œ ์—ฐ์‚ฐ๊ณผ ๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ ์ดํ›„์— ์ฒ˜๋ฆฌ๋  ์ฝ”๋“œ ์—ฐ์‚ฐ์„ ๋ถ„๋ฆฝ์‹œ์ผœ๋ณด๋‹ˆ ๋™์ž‘์˜ ๋ช…ํ™•์„ฑ๊ณผ ๋…๋ฆฝ์„ฑ์ด ๋‹ค์†Œ ๋–จ์–ด์ง„๋‹ค๋Š” ์ƒ๊ฐ์ด ๋“ค์–ด ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์„ ์ƒ๊ฐํ•ด ๋ณด์•˜๋‹ค.

 

axios Intercept requests

https://github.com/axios/axios#interceptors

 

 

GitHub - axios/axios: Promise based HTTP client for the browser and node.js

Promise based HTTP client for the browser and node.js - GitHub - axios/axios: Promise based HTTP client for the browser and node.js

github.com


You can intercept requests or responses before they are handled by then or catch.

axios ์ธํ„ฐ์…‰ํ„ฐ๋Š” ์š”์ฒญํ•˜๊ธฐ ์ง์ „, ์‘๋‹ต์„ ๋ฐ›๊ณ  then, catch๋กœ ์ฒ˜๋ฆฌ ์ง์ „์— ๊ฐ€๋กœ์ฑŒ ์ˆ˜ ์žˆ๋‹ค.

์ด๋ฅผ ์ด์šฉํ•˜์—ฌ loading ๊ด€๋ จ ๊ธฐ๋Šฅ์„ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ฆฌํŒฉํ† ๋ง ํ•ด๋ณด์•˜๋‹ค.

axios.interceptors.request.use(function (config) {
    $loadingIndicator.style.visibility = 'visible'; ์š”์ฒญ ์ง์ „์— loading UI๋ฅผ ๋ณด์—ฌ์ฃผ๊ธฐ
    return config;
}, function (error) {
    return Promise.reject(error);
})
๋Œ“๊ธ€
๊ณต์ง€์‚ฌํ•ญ
์ตœ๊ทผ์— ์˜ฌ๋ผ์˜จ ๊ธ€
์ตœ๊ทผ์— ๋‹ฌ๋ฆฐ ๋Œ“๊ธ€
Total
Today
Yesterday
๋งํฌ
TAG
more
ยซ   2025/01   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
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
๊ธ€ ๋ณด๊ด€ํ•จ