사용자 도구

사이트 도구


promise-prac

Promise Practice

Sample

async function wait1 (): Promise<void> {
    return new Promise((resolve) => {
        setTimeout(() => {
            console.log("WAIT1")
            resolve()
        }, 1000)
    })
}

async function wait2 (): Promise<void> {
    return new Promise((resolve) => {
        setTimeout(() => {
            console.log("WAIT2")
            resolve()
        }, 2000)
    })
}

async function wait3 (): Promise<void> {
    return new Promise((resolve) => {
        setTimeout(() => {
            console.log("WAIT3")
            resolve()
        }, 3000)
    })
}

async function waitForString (s: string, timeoutSec: number): Promise<[string, number]> {
    return new Promise((resolve) => {
        setTimeout(() => {
            console.log("Promise", s)
            resolve([s, timeoutSec])
        }, timeoutSec*1000)
    })
}

async function promisePrac2() {
    let second = 1
    setInterval(() => {
        console.log("Second: ", second++)
    }, 1000)
    const strings = ["a", "b", "c"]
    let x = 1
    const promises = strings.map(async s => await waitForString(s, x++))
    await Promise.all(promises)
}

async function promisePrac() {
    let second = 1
    setInterval(() => {
        console.log("Second: ", second++)
    }, 1000)
    // await wait1()
    // await wait2()
    // await wait3()
    const promises = [wait1(), wait2(), wait3()]
    await Promise.all([wait1(), wait2(), wait3()])
}

promisePrac2()
promise-prac.txt · 마지막으로 수정됨: 2021/04/13 07:17 저자 gsjung