ํ๊ณ
- ํ๋ก๊ทธ๋๋ฐ๋ ์ค์ํ์ง๋ง CS๊ฐ ๋ ์ค์ํ๋ค...
- ๊ทธ๊ฑธ ์๋ฉด์๋ ์ํด?!!, ์๊ฐ ๋ฐฐ๋ถ ์ํ์..
- D3.js ์ฌ๋ฐ๋ค...
- GitHub Pages์ userid.github.io๋ก static html ํ๋ ์ฌ๋ฆผ...
- ์์ฒญ ๊ฐํธํ๋ค...
- ๊ฐ๋ฐ์๋ค์ ๊ฐ๋ฐ์๊ฐ ๋์ด์ผ ์ด์๋จ๋๊ฑด๊ฐ...
JavaScript๐ซ
โจ How to use promises
- ์ดํ
- Modern JavaScript์์ asynchronous programming ํ๋๋ฐ ์์ด์ Promise ๋ชจ๋ฅด๋ฉด ์ฌ๋์ด ์๋๋ฏ๋ก... ๊ธฐ๋ณธ๊ฐ๋
์ ์ ์์งํ์.
- IE๋ ๋์๊ฐ์๊ณ ...
XMLHttpRequest
์ callback hell์ ํผํ ์ ์๋(Promise) ์๋์ ๊ฐ์ฌํ์.
- Table of Contents
- Overview
- Using the
fetch()
API
- Chanining promises
- Catching erros
- Promise terminology
- Combining multiple promises
async
and awiat
- Conclusion
- Punchline phrase/keyword
- pending, fullfilled, rejected, settled, resolved
- A promise is an object returned by an asynchronous function, which represents the current state of the operation.
- Code Snippets
const fetchPromise = fetch('https://mdn.github.io/learning-area/javascript/apis/fetching-data/can-store/products.json');
fetchPromise
.then(response => {
if (!response.ok) {
throw new Error(`HTTP error: ${response.status}`);
}
return response.json();
})
.then(data => {
console.log(data[0].name);
})
.catch(error => {
console.log(`Could not get products: ${error}`);
});
์ด๊ฒ์ ๊ฒ
- Callback function ์ด๋
- A callback function is a function passed into another function as an argument, which is then invoked inside the outer function to complete some kind of routine or action.
- Callbakcs are often used to continue code execution after an asynchronous operatin has completed - these are called asynchronous callbacks. A good example is the callback functions execued inside a
.then()
block chained onto the end of a promise after that promise fulfills or rejects. This structure is used in many modern web APIs, such as fetch()
.
- Synchronous callback example
function greeting(name) {
alert(`Hello, ${name}`);
}
function processUserInput(callback) {
var name = prompt('Please enter your name.);
callback(name);
}
processUserInput(greeting);