use strict'
// 异步计算,队列化,类比callback
// 都不是单纯的普通方法,都是promise对象才生效,对象可以保存状态,函数只有通过闭包才能保存状态
// 异步操作的常见方法:回调和事件监听
// 异步回调容易层次过深,可读性差,维护性差
// 异步回调剥夺了return的能力
// promise的三个状态:pending初始状态,fulfilled操作成功,rejected操作失败
// .then()返回新的promise实例,由此实现链式调用
// promise可以捕获错误
// 常和定时器一起使用
// new Promise (() => {
// console.log('支持promise')
// })
// let pro = new Promise(() => {})
// console.log(pro)
let randomPromise = function () {
let randNum = Math.random() * 2
console.log(randNum)
if(randNum < 1) {
setTimeout(() => {
console.log('Success Done')
}, randNum)
} else {
console.log('Failed')
}
}
let log = function (msg) {
console.log(msg)
}
// function test(resolve, reject) {
// var timeOut = Math.random() * 2;
// console.log('set timeout to: ' + timeOut + ' seconds.');
// setTimeout(function () {
// if (timeOut < 1) {
// console.log('call resolve()...');
// resolve('200 OK');
// }
// else {
// console.log('call reject()...');
// reject('timeout in ' + timeOut + ' seconds.');
// }
// }, timeOut * 1000);
// }
// randomPromise()
// test()
// var p1 = new Promise(test);
// var p2 = p1.then(function (result) {
// console.log('成功:' + result);
// });
// var p3 = p2.catch(function (reason) {
// console.log('失败:' + reason);
// });
let emotion = 'ff'
let happyOrNot = function (resolve, reject) {
if(emotion == 'happy') {
resolve('I am very happy')
} else {
reject('I am very sad')
}
}
// let promiseTest = new Promise(happyOrNot)
// let promiseThen = promiseTest.then(res => {
// console.log('成功呢:' + res)
// }).catch(err => {
// console.log('完蛋了:' + err)
// })
// 0.5秒后返回input*input的计算结果:
// function multiply(input) {
// return new Promise(function (resolve, reject) {
// log('calculating ' + input + ' x ' + input + '...');
// setTimeout(resolve, 500, input * input);
// });
// }
//
// // 0.5秒后返回input+input的计算结果:
// function add(input) {
// return new Promise(function (resolve, reject) {
// log('calculating ' + input + ' + ' + input + '...');
// setTimeout(resolve, 500, input + input);
// });
// }
//
// var p = new Promise(function (resolve, reject) {
// log('start new Promise...');
// resolve(123);
// });
//
// p.then(multiply)
// .then(add)
// .then(multiply)
// .then(add)
// .then(function (result) {
// log('Got value: ' + result);
// });
function first (text) {
return new Promise((resolve, reject) => {
let msg = '1 the text is:' + text
setTimeout(resolve, 500, msg)
})
}
function second (text) {
return new Promise((resolve, reject) => {
let msg = '2 the second text is:' + text
setTimeout(resolve, 500, msg)
})
}
function third (text) {
return new Promise((resolve, reject) => {
let msg = '3 the third text is:' + text
setTimeout(resolve, 500, msg)
})
}
let final = new Promise((resolve, reject) => {
console.log('start')
resolve('我在人民广场吃炸鸡')
})
// final.then(first).then(second).then(third).then(res => {
// console.log('最后的结果是:')
// console.log(res)
// })
let one = new Promise((resolve, reject) => {
console.log('one 1')
setTimeout(resolve, 500, 'one')
})
let two = new Promise((resolve, reject) => {
console.log('two 2')
setTimeout(resolve, 500, 'two')
})
Promise.all([one, two]).then(res => {
// 同时执行好几个方法
console.log('all')
console.log(res)
})
Promise.race([one, two]).then(res => {
// 容错,有一个结束就算完成
console.log('all')
console.log(res)
})
// async 返回的是promise对象
// async和await一起用,解决回调套层级问题
function getResult (num) {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve(2 * num)
}, 2000)
})
}
async function gotResult () {
let result = await getResult(8)
let result2 = await getResult(3)
let result3 = await getResult(7)
let finalResult = result + result2 + result3
console.log(finalResult)
return new Promise ((resolve, reject) => {
setTimeout(() => {
resolve(result * 2)
}, 2000)
})
}
gotResult().then(res => {
console.log(res)
})