Promise

Time: 2024-07-21 Sunday 14:49:01
Author: Jackasher

Promise

Promise的存在意义

就是来解决回掉地狱的问题,通过Resolve()来存取数据,then会在Promise状态为fullfill的时候执行,异步取数据的关键在什么时候取,直接取可能还没有数据

用promise解决回掉地狱

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
function sum(a,b){
return new Promise((resolve,reject) => {
setTimeout(() => {
resolve(a + b)
})
})
}

const result = sum(123,456)

const promise = result
.then((result) => {
return result += 7
})
.then((result) => {
return result += 8
})

setTimeout(() => {
console.log(promise);
},3000)

Promise
http://example.com/2024/07/21/Promise/
作者
Jack Asher
发布于
2024年7月21日
许可协议