<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
var arr = [1,2,3,4,5];
arr.forEach((item)=>{
console.log('settimeout之前');
setTimeout(function(){
console.log(item);
},3000);
console.log('settimeout之后');
})
console.log('promise');
</script>
</body>
</html>
//index.html:13 settimeout之前
//index.html:17 settimeout之后
//index.html:13 settimeout之前
//index.html:17 settimeout之后
//index.html:13 settimeout之前
//index.html:17 settimeout之后
//index.html:13 settimeout之前
//index.html:17 settimeout之后
/index.html:13 settimeout之前
//index.html:17 settimeout之后
//index.html:19 promise
//index.html:15 1
//index.html:15 2
//index.html:15 3
//index.html:15 4
//index.html:15 5
<script>
try {
let data ;
console.log(data.a);
} catch (error) {
console.log(error);
}
</script>
<script>
try {
let data = "errorData";
throw new Error(data);
console.log('data是否继续执行');
} catch (error) {
console.log('输出data错误信息');
console.log(error);
}
try {
let data1 = "errorData1";
throw (data1);
console.log('data1是否继续执行');
} catch (error) {
console.log('输出data错误信息');
console.log(error);
}
</script>
//index.html:18 输出data错误信息
//index.html:19 Error: errorData
// at index.html:15
//index.html:26 输出data错误信息
//index.html:27 errorData1
let promise = new Promise((resolve,reject)=>{
console.log('进入promise');
let data = 'promsie-data';
resolve(data);
// reject(data);
console.log('resove和reject之后');
}).then((res)=>{
console.log('resolve');
console.log('进入then');
console.log(res);
console.log('返回res');
return res;
}).catch((err)=>{
console.log('reject');
console.log('输出catch')
console.log(err);
console.log('返回err');
return err;
});
console.log('出了promsie');
promise.then((res)=>{
console.log('promise出来接着then');
console.log(res);
})
console.log('结束');
</script>
//index.html:14 进入promise
//index.html:18 resove和reject之后
//index.html:33 出了promsie
//index.html:38 结束
//index.html:20 resolve
//index.html:21 进入then
//index.html:22 promsie-data
//index.html:23 返回res
//index.html:35 promise出来接着then
//index.html:36 promsie-data
<script>
let promise = new Promise((resolve,reject)=>{
console.log('进入promise');
let data = 'promsie-data';
// resolve(data);
reject(data);
console.log('resove和reject之后');
}).then((res)=>{
console.log('resolve');
console.log('进入then');
console.log(res);
console.log('返回res');
return res;
}).catch((err)=>{
console.log('reject');
console.log('输出catch')
console.log(err);
console.log('返回err');
return err;
});
console.log('出了promsie');
promise.then((res)=>{
console.log('promise出来接着then');
console.log(res);
})
console.log('结束');
</script>
//index.html:14 进入promise
//index.html:18 resove和reject之后
//index.html:33 出了promsie
//index.html:38 结束
//index.html:26 reject
//index.html:27 输出catch
//index.html:28 promsie-data
//index.html:29 返回err
//index.html:35 promise出来接着then
//index.html:36 promsie-data
Promise.all()
Promise.allSettled()
Promise.any()
Promise.prototype.catch()
Promise.prototype.finally()
Promise.race()
Promise.reject()
Promise.resolve()
Promise.prototype.then()
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
(
function (window) {
function Promise(excutor) {
const self = this;
self.status = 'pending';
self.data = undefined;
self.callbacks = [];
function resolve(value) {
if (self.status !== 'pending') {
return
}
self.status = 'resolved';
self.data = value;
if (self.callbacks.length > 0) {
setTimeout(() => {
self.callbacks.forEach(callbacksObj => {
callbacksObj.onResolved(value);
});
});
}
}
function reject(res) {
if (self.status !== 'pending') {
return
}
self.status = 'rejected';
self.data = res;
if (self.callbacks.length > 0) {
setTimeout(() => {
self.callbacks.forEach(callbacksObj => {
callbacksObj.onRejected(value);
});
});
}
}
try {
excutor(resolve, reject);
} catch (error) {
reject(error);
}
}
Promise.prototype.then = function (onResolved, onRejected) {
onResolved = typeof onResolved === 'function' ? onResolved : value => value
onRejected = typeof onRejected === 'function' ? onRejected : res => { throw res }
const self = this;
return new Promise((resolve, reject) => {
function handle(callback) {
try {
const result = onResolved(self.data);
if (result instanceof Promise) {
// result.then(value => {
// resolve(value)
// }, res => {
// reject(res)
// })
result.then(resolve, reject)
}
else {
resolve(result);
}
} catch (error) {
reject(error)
}
}
if (self.status === "pending") {
self.callbacks.push({
onResolved() {
handle(onResolved)
}, onRejected() {
handle(onRejected)
}
})
}
if (self.status === "resolved") {
setTimeout(() => {
handle(onResolved)
})
}
if (self.status === "rejected") {
setTimeout(() => {
handle(onRejected)
})
}
})
}
Promise.prototype.catch = function (onRejected) {
return this.then(undefined, onRejected)
}
Promise.resolve = function (value) {
}
Promise.reject = function (res) {
}
Promise.all = function (promises) {
}
Promise.race = function (promises) {
}
window.Promise = Promise;
}
)(window)
let p = new Promise((resolve, reject) => {
setTimeout(() => {
resolve(2);
console.log('reject');
}, 100)
}).then(value => {
console.log('value', value);
console.log('value');
}, res => {
console.log('res');
console.log('res:', res)
})
// p.then(value => {
// console.log('value1', value);
// console.log('value1');
// }, res => {
// console.log('res1:', res)
// })
</script>
</body>
</html>
promise+setTimeout调用
let p = new Promise(resolve => {
setTimeout(function () {
resolve('succ')
},1000);
console.log('conso');
resolve('conso-----scc')
},reject=>{
reject('fail')
});
p.then((d)=>{
alert(d);
}).catch(e=>{
console.log(e);
})