//Promise链式编程
function city() {
return new Promise((resolve, reject) => {
setTimeout(() => {
console.log('hunansheng')
resolve('湖南')
}, 300);
})
}
function area(data) {
return new Promise((resolve, reject) => {
setTimeout(() => {
console.log('changshashi')
resolve('长沙')
}, 500);
})
}
function list(data) {
return new Promise((resolve, reject) => {
setTimeout(() => {
console.log('yuhuaqu')
resolve('雨花')
}, 800);
})
}
city().then(area).then(list).then((data) => {
console.log(data)
})
// async 和 await
async function f1() {
await fn()
await fs()
}
function fn() {
console.log("11")
}
function fs() {
console.log("333")
}
f1()
// 分两次顺序执行
new Promise(resolve => {
setTimeout(() => {
resolve('hello')
}, 2000)
}).then(val => {
console.log(val) // 参数val = 'hello'
return new Promise(resolve => {
setTimeout(() => {
resolve('world')
}, 2000)
})
}).then(val => {
console.log(val) // 参数val = 'world'
})
网友评论