TIL/자바스크립트(18)
-
callback to promise
// Callback Hell example class UserStorage { loginUser(id, password) { return new Promise((resolve, reject) => { setTimeout(()=> { if ( (id === 'bella' && password === 'dream') || (id === 'coder' && password === 'academy') ) { resolve(id); } else { reject(new Error('not found')); } }, 2000); }); } getRoles(user) { return new Promise((resolve, reject) => { setTimeout(()=> { if (user === 'bella') ..
2021.05.24 -
Promise
// Promise is a JavaScript object for asynchronous operation. // State: pending -> fulfilled or rejected // Producer vs Consumer // 1. Producer // when new Promis is created, the executor runs automatically. const promise = new Promise((resolve, reject) => { // doing some heavy work (network, read files) console.log('doing something...'); setTimeout(() => { resolve('bella'); reject(new Error('no..
2021.05.24 -
비동기처리 콜백
// JavaScript is synchronous. // Execute the code block in oreder after hoisting. // hoisting: var, function declaration console.log('1'); setTimeout(() => console.log('2'), 1000 ); console.log('3'); // Synchronous callback function printImmediately(print) { print(); } printImmediately(() => console.log('hello')); // Asynchronous callback function printWithDelay(print, timeout) { setTimeout(prin..
2021.05.20 -
JSON
// 1. Object to JSON // stringify(obj) let json = JSON.stringify(true); console.log(json); json = JSON.stringify(['apple', 'banana']); console.log(json); const rabbit = { name: 'tori', color: 'white', sisze: null, birthDate: new Date(), jump: () => { console.log(`${name} can jump!`); }, }; json = JSON.stringify(rabbit); console.log(json); json = JSON.stringify(rabbit, ['name', 'color', 'size']);..
2021.05.20 -
Array API Quiz
// Q1. make a string out of an array { const fruits = ['apple', 'banana', 'orange']; const result = fruits.join('|'); console.log(result); } // Q2. make an array out of a string { const fruits = '🍎, 🥝, 🍌, 🍒'; const result = fruits.split(','); console.log(result); } // Q3. make this array look like this: [5, 4, 3, 2, 1] { const array = [1, 2, 3, 4, 5]; const result = array.reverse(); //reverse: 기..
2021.05.20 -
Array 배열
// 1. Declaration const arr1 = new Array(); const arr2 = [1, 2]; // 2. Index position const fruits = ['🍎', '🍌']; console.log(fruits); console.log(fruits.length); console.log(fruits[0]); console.log(fruits[1]); console.log(fruits[2]); console.log(fruits[fruits.length - 1]); console.clear(); // 3. Looping over an array // print all fruits // a. for for (let i = 0; i < fruits.length; i++) { console..
2021.05.18