【前端,TypeScript】TypeScript速成(八):Promise

Promise

前端编程是一个单线程的模型,但是其中包含许多异步的动作,异步的动作应该通过以下四步来完成:

  • 发起请求;
  • 事件处理函数结束;
  • 请求结束;
  • 进入回调函数;

上述的基于回调函数解决方案不够好,当事件请求过多时,可能会产生 Callback Hell 现象。使用 Promise 可以解决回调函数的问题。

Promise 的创建和使用

我们首先来看一个回调函数的例子,在这个例子中,我们在回调函数中进行简单的 number 加法:

typescript 复制代码
function add(a: number, b: number, callback: (res: number) => void): void {
    setTimeout(() => {
        callback(a + b)
    }, 2000)
}

add(2, 3, res => {
    console.log('2 + 3', res)
    add(res, 4, res2 => {
        console.log('2 + 3 + 4', res2)
    })
})

程序将先后输出:

typescript 复制代码
[LOG]: "2 + 3",  5 
[LOG]: "2 + 3 + 4",  9 

现在我们将回调函数改为 Promise:

typescript 复制代码
function add(a: number, b: number): Promise<number> {
    return new Promise(
        (resolve, reject) => {
            setTimeout(() => {
                resolve(a + b)
            }, 2000)
        }
    )
}

add(2, 3).then(res => {
    console.log('2 + 3', res)
})

可以使用 then 方法来完成多个计算结果的顺序打印:

typescript 复制代码
function add(a: number, b: number): Promise<number>{
    return new Promise(
        (resolve, reject) => {
            if(b % 17 == 0) {
                reject(`bad number ${b}`)
            }
            setTimeout(
                () => {
                    resolve(a + b)
                }, 2000
            )
        }
    )
}

function mul(a: number, b: number): Promise<number>{
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            resolve(a * b)
        }, 3000)
        // resolve(a * b)
    })
}

// (2 + 3) * 4 + 5
add(2, 3).then(res => {
    console.log("2 + 3", res)
    return mul(res, 5)
}).then(res => {
    console.log("(2 + 3) * 4", res)
    return add(res, 5)
}).then(res => {
    console.log("(2 + 3) * 4 + 5", res)
    return add(res, 17)
}).then(res => {
    console.log(res)
}).catch(err => {
    console.log(err)
})

输出的结果是:

typescript 复制代码
[LOG]: "2 + 3",  5 
[LOG]: "(2 + 3) * 4",  25 
[LOG]: "(2 + 3) * 4 + 5",  30 
[LOG]: "bad number 17" 

需要注意的是,在使用 then 方法时,一定要在 then 作为参数的箭头函数当中将下一步要做的行为返回出去,这样才能创建多个 Promise。

同时等待多个 Promise

现在我们希望通过 Promise 来计算(2 + 3) * (4 + 5)的结果,使用 Promise.all 方法来完成:

typescript 复制代码
// (2+3)*(4+5)
Promise.all([add(2, 3), add(4, 5)]).then(res => {
    let [a, b] = res
    return mul(a, b)
}).then(res => {
    console.log("(2+3)*(4+5)", res)
})

可以使用 Promise.race 来看哪个 Promise 首先执行完毕:

typescript 复制代码
Promise.race([add(2, 3), add(4, 5)]).then(res => {
    console.log(res)
})

Promise 最大的用处是将多种行为串联起来,可以解决 Callback Hell 的问题。

相关推荐
在人间耕耘8 小时前
鸿蒙应用开发:WebSocket 使用示例
typescript
在人间耕耘8 小时前
uni-app/uniappx 中调用鸿蒙原生扫码能力的实践
typescript
海的诗篇_17 小时前
前端开发面试题总结-JavaScript篇(二)
开发语言·前端·javascript·typescript
dancing9991 天前
cocos3.X的oops框架oops-plugin-excel-to-json改进兼容多表单导出功能
前端·javascript·typescript·游戏程序
yinke小琪1 天前
快速开始 - TypeScript 入门指南
前端·typescript
wordbaby2 天前
🎯 satisfies 关键字详解(TypeScript)
前端·typescript
SailingCoder2 天前
grafana-mcp-analyzer:基于 MCP 的轻量 AI 分析监控图表的运维神器!
运维·人工智能·typescript·node.js·grafana
狂炫一碗大米饭2 天前
一文打通TypeScript 泛型
前端·javascript·typescript
Dignity_呱3 天前
别在傻傻分不清any void never unknown的场景啦
前端·vue.js·typescript
烛阴3 天前
模块/命名空间/全局类型如何共存?TS声明空间终极生存指南
前端·javascript·typescript