๐Ÿ˜Ž
Onemorebottlee's TIL
  • ๐Ÿค“Introduce
  • ๐Ÿ“–DevLog
    • ๐Ÿ’ฃError
      • npm ERR! Cannot read properties of null (reading 'edgesOut')
      • git
        • src refspec main does not match any
      • NextJS
        • No img element
        • ReferenceError : document is not defined
        • `next/image` Un-configured Host
  • ๐Ÿ“บLecture
    • Nomad Coders
      • ์ดˆ๋ณด์ž๋ฅผ ์œ„ํ•œ ๋ฆฌ๋•์Šค 101
        • #0 Introduction
        • #1 PURE REDUX: COUNTER
        • #2 PURE REDUX: TO DO LIST
        • #3 REACT REDUX
        • #4 Redux Toolkit
      • ๋ฐ”๋‹๋ผ JS๋กœ ๊ทธ๋ฆผ ์•ฑ ๋งŒ๋“ค๊ธฐ
      • React JS ๋งˆ์Šคํ„ฐ ํด๋ž˜์Šค
        • #2 Styled Componnents
        • #3 TypeScript
        • #4 Crypto Tracker
        • #5 State Management
        • #6 Trello
        • #7 ANIMATIONS
      • Next.js ์‹œ์ž‘ํ•˜๊ธฐ
        • #0 INTRODUCTION
        • #1 FRAMEWORK OVERVIEW
        • #2 PRACTICE PROJECT
      • Typescript๋กœ ๋ธ”๋ก์ฒด์ธ ๋งŒ๋“ค๊ธฐ
        • #1 Introduction
        • #2 Overview of TypeScript
        • #3 Functions
        • #4 Classes and Interfaces
        • #5 TypeScript Blockchain
      • SQL Master Class
        • #1 Introduction
        • #2 SQLite
        • #3 Data Definition Language
        • #4 Data Manipulation Language
        • #5 Subqueries and CTEs
        • #6 Indexes
        • #7 MySQL
        • #8 Foreign Keys
        • #9 JOINS
        • #10 Nomalization
        • #11 Events & Triggers
        • #12 Full-Text Indexes
        • #13 PostgreSQL
        • #14 Functions And Procedures
        • #15 Transactions
        • #16 Data Control Language
        • #17 PostgreSQL JSON Columns
        • #18 PostgreSQL Extensions
        • #19 MongoDB
        • #20 REDIS
        • #21 Javascript and Python Drivers
    • Udemy
      • TypeScript ๋งˆ์Šคํ„ฐ with Webpack & React
        • [S1] ์†Œ๊ฐœ
        • [S2] ์„ค์น˜ ๋ฐ ์„ค์ •
        • [S3] ํƒ€์ž… ์• ๋„ˆํ…Œ์ด์…˜ ๊ธฐ์ดˆ
        • [S4] ํ•จ์ˆ˜
        • [S5] ๊ฐ์ฒด ํƒ€์ž…
        • [S6] ๋ฐฐ์—ด ํƒ€์ž…
        • [S7] ์œ ๋‹ˆ์˜จํƒ€์ž…
        • [S8] Tuple & Enum
        • [S9] ์ธํ„ฐํŽ˜์ด์Šค
        • [S10] TypeScript ์ปดํŒŒ์ผ๋Ÿฌ
        • [S11] ๋ฏธ๋‹ˆ ํ”„๋กœ์ ํŠธ DOM, ํƒ€์ž… ๋‹จ์–ธ, ๊ทธ๋ฆฌ๊ณ  ๋” ๋งŽ์€ ๋‚ด์šฉ
        • [S12] Class
        • [S13] TS Class
        • [S14] Generics โญโญโญโญโญ
        • [S15] Narrowing
        • [S16] Type Declarations
        • [S17] Module
        • [S18] Webpack ๊ณผ TypeScript
        • [S19] React
    • ๋ชจ๋‘๋ฅผ ์œ„ํ•œ ์ปดํ“จํ„ฐ ๊ณผํ•™ CS50
      • 1. ์ปดํ“จํŒ… ์‚ฌ๊ณ 
      • 2. C์–ธ์–ด
      • 3. ๋ฐฐ์—ด
      • 4. ์•Œ๊ณ ๋ฆฌ์ฆ˜
      • 5. ๋ฉ”๋ชจ๋ฆฌ
      • 6. ์ž๋ฃŒ๊ตฌ์กฐ
    • ์ƒํ™œ์ฝ”๋”ฉ
      • DATABASE
        • DATABASE1
  • ๐Ÿ“šBook
    • ๋ชจ๋˜ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ Deep Dive
      • 1์žฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ
      • 2์žฅ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋ž€?
      • 3์žฅ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๊ฐœ๋ฐœ ํ™˜๊ฒฝ๊ณผ ์‹คํ–‰ ๋ฐฉ๋ฒ•
      • 4์žฅ ๋ณ€์ˆ˜
      • 5์žฅ ํ‘œํ˜„์‹๊ณผ ๋ฌธ
      • 6์žฅ ๋ฐ์ดํ„ฐ ํƒ€์ž…
      • 7์žฅ ์—ฐ์‚ฐ์ž
      • 8์žฅ ์ œ์–ด๋ฌธ
      • 9์žฅ ํƒ€์ž… ๋ณ€ํ™˜๊ณผ ๋‹จ์ถ• ํ‰๊ฐ€ โญโญโญ
      • 10์žฅ ๊ฐ์ฒด ๋ฆฌํ„ฐ๋Ÿด
      • 11์žฅ ์›์‹œ ๊ฐ’๊ณผ ๊ฐ์ฒด์˜ ๋น„๊ต
      • 12์žฅ ํ•จ์ˆ˜ โญโญโญโญ
      • 13์žฅ ์Šค์ฝ”ํ”„
      • 14์žฅ ์ „์—ญ ๋ณ€์ˆ˜์˜ ๋ฌธ์ œ์ 
      • 15์žฅ let, const ํ‚ค์›Œ๋“œ์™€ ๋ธ”๋ก ๋ ˆ๋ฒจ ์Šค์ฝ”ํ”„ โญโญโญ
      • 16์žฅ ํ”„๋กœํผํ‹ฐ ์–ดํŠธ๋ฆฌ๋ทฐํŠธ
      • 17์žฅ ์ƒ์„ฑ์ž ํ•จ์ˆ˜์— ์˜ํ•œ ๊ฐ์ฒด ์ƒ์„ฑ
      • 18์žฅ ํ•จ์ˆ˜์™€ ์ผ๊ธ‰ ๊ฐ์ฒด โญโญ
      • 19์žฅ ํ”„๋กœํ† ํƒ€์ž… โญโญโญโญโญ
      • 20์žฅ strict mode
      • 21์žฅ ๋นŒํŠธ์ธ ๊ฐ์ฒด โญ
      • 22์žฅ this โญโญโญ
      • 23์žฅ ์‹คํ–‰ ์ปจํ…์ŠคํŠธ โญโญโญโญโญโญโญโญโญโญ
      • 24์žฅ ํด๋กœ์ € โญโญโญโญโญโญโญ
      • 25์žฅ ํด๋ž˜์Šค
      • 26์žฅ ES6 ํ•จ์ˆ˜์˜ ์ถ”๊ฐ€ ๊ธฐ๋Šฅ
      • 27์žฅ ๋ฐฐ์—ด โญโญโญ
      • 28์žฅ Number
      • 29์žฅ Math
      • 30์žฅ Date
      • 31์žฅ RegExp
      • 32์žฅ String
      • 33์žฅ 7๋ฒˆ์งธ ๋ฐ์ดํ„ฐ ํƒ€์ž… Symbol
      • 34์žฅ ์ดํ„ฐ๋Ÿฌ๋ธ”
      • 35์žฅ ์Šคํ”„๋ ˆ๋“œ ๋ฌธ๋ฒ•
      • 36์žฅ ๋””์ŠคํŠธ๋Ÿญ์ฒ˜๋ง ํ• ๋‹น
      • 37์žฅ Set๊ณผ Map
      • 38์žฅ ๋ธŒ๋ผ์šฐ์ €์˜ ๋ Œ๋”๋ง ๊ณผ์ •โญโญโญโญโญโญโญโญ
      • 39์žฅ DOM โญโญโญโญโญโญโญโญโญโญโญ
      • 40์žฅ ์ด๋ฒคํŠธโญโญโญโญโญโญโญโญโญโญโญ
      • 41์žฅ ํƒ€์ด๋จธ
      • 42์žฅ ๋น„๋™๊ธฐ ํ”„๋กœ๊ทธ๋ž˜๋ฐ โญโญโญโญโญโญโญโญ
      • 43์žฅ Ajax
      • 44์žฅ REST API
      • 45์žฅ ํ”„๋กœ๋ฏธ์Šค
      • 46์žฅ ์ œ๋„ˆ๋ ˆ์ดํ„ฐ์™€ async/await
      • 47์žฅ ์—๋Ÿฌ ์ฒ˜๋ฆฌ
      • 48์žฅ ๋ชจ๋“ˆ
      • 49์žฅ Babel๊ณผ Webpack์„ ์ด์šฉํ•œ ES6+/ES.NEXT ๊ฐœ๋ฐœ ํ™˜๊ฒฝ ๊ตฌ์ถ•
    • ๋Ÿฌ๋‹ ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ
      • Ch.1 ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ๋กœ
      • Ch.2 ํƒ€์ž… ์‹œ์Šคํ…œ
      • Ch.3 ์œ ๋‹ˆ์–ธ๊ณผ ๋ฆฌํ„ฐ๋Ÿด
      • Ch.4 ๊ฐ์ฒด
      • Ch.5 ํ•จ์ˆ˜
      • Ch.6 ๋ฐฐ์—ด
      • Ch.7 ์ธํ„ฐํŽ˜์ด์Šค
      • Ch.8 ํด๋ž˜์Šค
      • Ch.9 ํƒ€์ž… ์ œํ•œ์ž
      • Ch.10 ์ œ๋„ค๋ฆญ
      • Ch.11 ์„ ์–ธ ํŒŒ์ผ
      • Ch.12 IDE ๊ธฐ๋Šฅ ์‚ฌ์šฉ
      • Ch.13 ๊ตฌ์„ฑ ์˜ต์…˜
      • Ch.14 ๊ตฌ๋ฌธ ํ™•์žฅ
      • Ch.15 ํƒ€์ž… ์šด์˜
      • ์šฉ์–ด ์‚ฌ์ „
    • ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์™„๋ฒฝ ๊ฐ€์ด๋“œ
    • SQL in 10 Minutes
      • 1์žฅ SQL ์ดํ•ดํ•˜๊ธฐ
      • 2์žฅ ๋ฐ์ดํ„ฐ ๊ฐ€์ ธ์˜ค๊ธฐ
      • 3์žฅ ๊ฐ€์ ธ์˜จ ๋ฐ์ดํ„ฐ ์ •๋ ฌํ•˜๊ธฐ
      • 4์žฅ ๋ฐ์ดํ„ฐ ํ•„ํ„ฐ๋ง
      • 5์žฅ ๊ณ ๊ธ‰ ๋ฐ์ดํ„ฐ ํ•„ํ„ฐ๋ง
      • 6์žฅ ์™€์ผ๋“œ์นด๋“œ ๋ฌธ์ž๋ฅผ ์ด์šฉํ•œ ํ•„ํ„ฐ๋ง
      • 7์žฅ ๊ณ„์‚ฐ ํ•„๋“œ ์ƒ์„ฑํ•˜๊ธฐ
      • 8์žฅ ๋ฐ์ดํ„ฐ ์กฐ์ž‘ ํ•จ์ˆ˜ ์‚ฌ์šฉํ•˜๊ธฐ
      • 9์žฅ ๋ฐ์ดํ„ฐ ์š”์•ฝ
      • 10์žฅ ๋ฐ์ดํ„ฐ ๊ทธ๋ฃนํ•‘
      • 11์žฅ ์„œ๋ธŒ์ฟผ๋ฆฌ ์‚ฌ์šฉํ•˜๊ธฐ
      • 12์žฅ ํ…Œ์ด๋ธ” ์กฐ์ธ
      • 13์žฅ ๊ณ ๊ธ‰ ํ…Œ์ด๋ธ” ์กฐ์ธ ์ƒ์„ฑํ•˜๊ธฐ
      • 14์žฅ ์ฟผ๋ฆฌ ๊ฒฐํ•ฉํ•˜๊ธฐ
      • 15์žฅ ๋ฐ์ดํ„ฐ ์‚ฝ์ž…ํ•˜๊ธฐ
      • 16์žฅ ๋ฐ์ดํ„ฐ ์—…๋ฐ์ดํŠธ์™€ ์‚ญ์ œ
      • 17์žฅ ํ…Œ์ด๋ธ” ์ƒ์„ฑ๊ณผ ์กฐ์ž‘
      • 18์žฅ ๋ทฐ ์‚ฌ์šฉํ•˜๊ธฐ
      • 19์žฅ ์ €์žฅ ํ”„๋กœ์‹œ์ € ์‚ฌ์šฉํ•˜๊ธฐ
      • 20์žฅ ํŠธ๋žœ์žญ์…˜ ์ฒ˜๋ฆฌ ๊ด€๋ฆฌํ•˜๊ธฐ
      • 21์žฅ ์ปค์„œ ์‚ฌ์šฉํ•˜๊ธฐ
      • 22์žฅ ๊ณ ๊ธ‰ ๋ฐ์ดํ„ฐ ์กฐ์ž‘ ์˜ต์…˜
    • ๋ฉด์ ‘์„ ์œ„ํ•œ CS ์ „๊ณต์ง€์‹ ๋…ธํŠธ
      • 4 ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค
        • 4.1 ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์˜ ๊ธฐ๋ณธ
    • 2023 ์ด๊ธฐ์  SQL ๊ฐœ๋ฐœ์ž ์ด๋ก ์„œ + ๊ธฐ์ถœ๋ฌธ์ œ
      • ๋ฐ์ดํ„ฐ ๋ชจ๋ธ๋ง ์ดํ•ด
        • S1. ๋ฐ์ดํ„ฐ ๋ชจ๋ธ๋ง
  • ๐Ÿ’ปStudy
    • CS ์ง€์‹ ๋ฐœํ‘œ
      • 1ํšŒ์ฐจ
      • 2ํšŒ์ฐจ
      • 3ํšŒ์ฐจ
      • 4ํšŒ์ฐจ
      • 5ํšŒ์ฐจ
      • 6ํšŒ์ฐจ
      • 7ํšŒ์ฐจ
      • 8ํšŒ์ฐจ
      • 9ํšŒ์ฐจ
    • TypeScript Exercises
      • Ex 1
      • Ex 2
      • Ex 3
      • Ex 4
      • Ex 5
      • Ex 6
      • Ex 7
      • Ex 8
      • Ex 9
      • Ex 10
  • ๐Ÿ”„ETC
    • Article
      • Atomic Design Pattern
      • ํ”„๋ก ํŠธ์—”๋“œ ๊ฐœ๋ฐœ์ž
    • DATABASE
      • Oracle
        • CEIL() & FLOOR() - ์†Œ์ˆ˜์  ์˜ฌ๋ฆผ & ๋‚ด๋ฆผ
        • DUAL ํ…Œ์ด๋ธ” - ์ž„์‹œ ํ…Œ์ด๋ธ”๋กœ ๊ฒฐ๊ณผ ํ™•์ธํ•˜๊ธฐ
    • Ubuntu
      • ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜ ์—…๋ฐ์ดํŠธ
Powered by GitBook
On this page
  • ๋ฌธ์ œ
  • ํ’€์ด

Was this helpful?

  1. Study
  2. TypeScript Exercises

Ex 10

๋ฌธ์ œ

/*

Intro:

    We have asynchronous functions now, advanced technology.
    This makes us a tech startup officially now.
    But one of the consultants spoiled our dreams about
    inevitable future IT leadership.
    He said that callback-based asynchronicity is not
    popular anymore and everyone should use Promises.
    He promised that if we switch to Promises, this would
    bring promising results.

Exercise:

    We don't want to reimplement all the data-requesting
    functions. Let's decorate the old callback-based
    functions with the new Promise-compatible result.
    The final function should return a Promise which
    would resolve with the final data directly
    (i.e. users or admins) or would reject with an error
    (or type Error).

    The function should be named promisify.

Higher difficulty bonus exercise:

    Create a function promisifyAll which accepts an object
    with functions and returns a new object where each of
    the function is promisified.

    Rewrite api creation accordingly:

        const api = promisifyAll(oldApi);

*/

interface User {
    type: 'user';
    name: string;
    age: number;
    occupation: string;
}

interface Admin {
    type: 'admin';
    name: string;
    age: number;
    role: string;
}

type Person = User | Admin;

const admins: Admin[] = [
    { type: 'admin', name: 'Jane Doe', age: 32, role: 'Administrator' },
    { type: 'admin', name: 'Bruce Willis', age: 64, role: 'World saver' }
];

const users: User[] = [
    { type: 'user', name: 'Max Mustermann', age: 25, occupation: 'Chimney sweep' },
    { type: 'user', name: 'Kate Mรผller', age: 23, occupation: 'Astronaut' }
];

export type ApiResponse<T> = (
    {
        status: 'success';
        data: T;
    } |
    {
        status: 'error';
        error: string;
    }
);

export function promisify(arg: unknown): unknown {
    return null;
}

const oldApi = {
    requestAdmins(callback: (response: ApiResponse<Admin[]>) => void) {
        callback({
            status: 'success',
            data: admins
        });
    },
    requestUsers(callback: (response: ApiResponse<User[]>) => void) {
        callback({
            status: 'success',
            data: users
        });
    },
    requestCurrentServerTime(callback: (response: ApiResponse<number>) => void) {
        callback({
            status: 'success',
            data: Date.now()
        });
    },
    requestCoffeeMachineQueueLength(callback: (response: ApiResponse<number>) => void) {
        callback({
            status: 'error',
            error: 'Numeric value has exceeded Number.MAX_SAFE_INTEGER.'
        });
    }
};

export const api = {
    requestAdmins: promisify(oldApi.requestAdmins),
    requestUsers: promisify(oldApi.requestUsers),
    requestCurrentServerTime: promisify(oldApi.requestCurrentServerTime),
    requestCoffeeMachineQueueLength: promisify(oldApi.requestCoffeeMachineQueueLength)
};

function logPerson(person: Person) {
    console.log(
        ` - ${person.name}, ${person.age}, ${person.type === 'admin' ? person.role : person.occupation}`
    );
}

async function startTheApp() {
    console.log('Admins:');
    (await api.requestAdmins()).forEach(logPerson);
    console.log();

    console.log('Users:');
    (await api.requestUsers()).forEach(logPerson);
    console.log();

    console.log('Server time:');
    console.log(`   ${new Date(await api.requestCurrentServerTime()).toLocaleString()}`);
    console.log();

    console.log('Coffee machine queue length:');
    console.log(`   ${await api.requestCoffeeMachineQueueLength()}`);
}

startTheApp().then(
    () => {
        console.log('Success!');
    },
    (e: Error) => {
        console.log(`Error: "${e.message}", but it's fine, sometimes errors are inevitable.`);
    }
);

// In case you are stuck:
// https://www.typescriptlang.org/docs/handbook/2/generics.html

ํ’€์ด

api ๊ฐ์ฒด๋ฅผ ๋ณด๋ฉด 4๊ฐœ์˜ ์†์„ฑ๋“ค์ด promisify์˜ ๊ฒฐ๊ณผ๊ฐ’์„ ์˜๋ฏธํ•˜๊ณ  ์žˆ๋‹ค. ๋”ฐ๋ผ์„œ oldApi๋ฅผ ํ•œ ๊ณณ์—์„œ ๊ด€๋ฆฌํ•  ํ•จ์ˆ˜ promisify์˜ ํƒ€์ž…์„ ์ง€์ •ํ•ด์•ผํ•œ๋‹ค.

๋จผ์ €, ๋งค๊ฐœ๋ณ€์ˆ˜์˜ ๊ฐ’๊ณผ ๋ฆฌํ„ด๊ฐ’์˜ ํƒ€์ž…์ด ๊ฐ๊ฐ ๋‹ค๋ฅด๊ธฐ ๋•Œ๋ฌธ์— ์ œ๋„ค๋ฆญ์„ ์‚ฌ์šฉํ•ด ํƒ€์ž…์„ ์ง€์ •ํ•ด์ค€๋‹ค. ๋ฆฌํ„ด๊ฐ’์˜ ๊ฒฝ์šฐ, ๋ฌธ์ œ์— ์ ํžŒ ๋Œ€๋กœ Promise ๋ฅผ ์ถœ๋ ฅํ•˜๊ฒŒ๋” ์ง€์ •ํ•œ๋‹ค.

export function promisify<T>(arg: unknown): () => Promise<T> {
    return () => new Promise((resolve, reject) => {
        // resolve ์ผ๋•Œ
        // else reject ์ผ๋•Œ
    })
}

๊ทธ๋ฆฌ๊ณ  api ์—์„œ promisify ํ•จ์ˆ˜์— ์ œ๋„ค๋ฆญ ํƒ€์ž…์„ ์ถ”๊ฐ€ํ•ด์ฃผ๋ฉด ๋ฌธ์ œ๋Š” ํ•ด๊ฒฐ๋œ๋‹ค.

export const api = {
    requestAdmins: promisify<Admin[]>(oldApi.requestAdmins),
    requestUsers: promisify<User[]>(oldApi.requestUsers),
    requestCurrentServerTime: promisify<number>(oldApi.requestCurrentServerTime),
    requestCoffeeMachineQueueLength: promisify<number>(oldApi.requestCoffeeMachineQueueLength)
};

+

๋ณด๋„ˆ์Šค ๋ฌธ์ œ์ธ promisifyAll ๊ตฌํ˜„์€ ๋‚˜์ค‘์— ๋‹ค์‹œ ์‹œ๋„ํ•ด๋ณด์ž

type CallbackBasedAsyncFunction<T> = (callback: (response: ApiResponse<T>) => void) => void;
type PromiseBasedAsyncFunction<T> = () => Promise<T>;

export function promisify<T>(fn: CallbackBasedAsyncFunction<T>): PromiseBasedAsyncFunction<T> {
    return () => new Promise<T>((resolve, reject) => {
        fn((response) => {
            if (response.status === 'success') {
                resolve(response.data);
            } else {
                reject(new Error(response.error));
            }
        });
    });
}

type SourceObject<T> = {[K in keyof T]: CallbackBasedAsyncFunction<T[K]>};
type PromisifiedObject<T> = {[K in keyof T]: PromiseBasedAsyncFunction<T[K]>};

export function promisifyAll<T extends {[key: string]: any}>(obj: SourceObject<T>): PromisifiedObject<T> {
    const result: Partial<PromisifiedObject<T>> = {};
    for (const key of Object.keys(obj) as (keyof T)[]) {
        result[key] = promisify(obj[key]);
    }
    return result as PromisifiedObject<T>;
}

PreviousEx 9NextArticle

Last updated 1 year ago

Was this helpful?

๐Ÿ’ป