/*//1.promise的立即执行性
const p = new Promise((resolve, reject) => {
console.log('create a promise')
resolve('success')
})
p.then(value => console.log(value))
console.log('after new Promise')
//2.promise状态的不可逆性
const p1 = new Promise((resolve, reject) => {
resolve('success1')
resolve('success2')
})
const p2 = new Promise((resolve, reject) => {
resolve('success')
reject('reject')
})
p1.then(value => console.log(value))
p2.then(value => console.log(value))
//3.链式调用
const p3 = new Promise((resolve, reject) => {
resolve(1)
})
p3.then(value => {
console.log(value)
return value * 2
}).then(value => {
console.log(value)
}).then(value => {
console.log(typeof value)
return Promise.resolve('resolve')
}).then(value => {
console.log(value)
return Promise.reject('reject')
}).then(value => {
console.log('resolve:'+ value)
}, err => console.log('reject:'+ err))
*/
//4.promise中的异常
const p4 = new Promise((resolve, reject) => {
foo.bar()
resolve(1)
})
p4.then(value => {
console.log('p4 then value:' + value)
}, err => {
console.log('p4 then err:' + err)
}).then(value => {
console.log('p4 then then value:' + value)
}, err => {
console.log('p4 then then err:' + err)
})
const p5 = new Promise((resolve, reject) => {
resolve(2)
})
p5.then(value => {
console.log('p5 then value:' + value)
foo.bar()
}, err => {
console.log('p5 then err:' + err)
}).then(value => {
console.log('p5 then then value:' + value)
}, err => {
console.log('p5 then then err:' + err)
return 1
}).then(value => {
console.log('p5 then then then value:' + value)
}, err => {
console.log('p5 then then then err:' + err)
})
/*
//5.Promise.all方法
let arr = [1, 2, 3].map(
(value) => {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve(value);
}, value * 1000);
});
}
);
console.log(arr);
let promises = Promise.all(arr)
.then((result) => {
console.log(result);
}).catch((err) => {
console.log(err);
});
*/
//6.promise.race方法
let arr_1 = [1, 2, 3].map(
(value) => {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve(value);
}, value * 1000);
});
}
);
let promises_1 = Promise.race(arr_1)
.then((result) => {
console.log(result);
}).catch((err) => {
console.log(err);
});
console