TIL-20220802

__flow__ยท2022๋…„ 8์›” 2์ผ
0

TIL

๋ชฉ๋ก ๋ณด๊ธฐ
28/49
post-thumbnail

ํšŒ๊ณ 


  • ํ”„๋กœ๊ทธ๋ž˜๋ฐ๋„ ์ค‘์š”ํ•˜์ง€๋งŒ 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);

profile
fullcycle(fullstack), python/javascript, keepflowin, he/him

0๊ฐœ์˜ ๋Œ“๊ธ€