const p = new Promise(() => {});
const p = new Promise((resolve, reject) => {
// pending->fulfilled
resolve();
// pending->rejected
reject();
});
p.then(
() => {
console.log('success');
},
() => {
console.log('error');
}
);
const p = new Promise((resolve, reject) => {
// resolve('succ');
// 也可以传一些数据
resolve({ username: 'mike' });
reject('reason');
// 比较常用的是传一个错误对象
reject(new Error('reason'));
});
p.then(
// 在 then 方法中接收数据
data => {
console.log('success', data); //success {username: "mike"}
},
err => {
console.log('error', err); // error reason
}
);
const p = new Promise((resolve, reject) => {
resolve();
// reject();
});
const p2 = p
.then(
() => {},
() => {}
)
.then()
.then();
const p = new Promise((resolve, reject) => {
resolve();
// reject();
});
p.then(
() => {
console.log('success');
},
() => {
console.log('err');
// 在 then 的回调函数中,return 后面的东西,会用 Promise 包装一下
// return undefined;
// 等价于
// return new Promise(resolve => {
// resolve(undefined);
// });
// return 123;
// 等价于
// return new Promise(resolve => {
// resolve(123);
// });
// 默认返回的永远都是成功状态的 Promise 对象
// 主动设置为失败
// return new Promise((resolve, reject) => {
// reject('reason');
// });
}
)
.then(
data => {
console.log('success2', data);
// return undefined;
// 默认
return new Promise(resolve => {
resolve(undefined);
});
},
err => {
console.log('err2', err);
}
)
.then(
data => {
console.log('success3', data);
},
err => {
console.log('err3', err);
}
);
then(
data => {},
err => {}
);
// 一般只写第一个回调用来处理成功的...
then(data => {});
// 可以理解为用 then 专门处理成功
所以用 catch 专门用来处理 rejected 状态
catch 本质上是 then 的特例 then(null, err => {});
new Promise((resolve, reject) => {
resolve(123);
reject('reason');
})
.then(data => {
console.log(data);
})
// .then(null, err => {
// console.log(err);
// });
.catch(err => {
console.log(err);
// return undefined;
throw new Error('reason');
})
.then(data => {
console.log(data);
})
.catch(err => {
console.log(err);
});
// catch() 可以捕获它前面的错误
// 一般总是建议,Promise 对象后面要跟 catch 方法,这样可以处理 Promise 内部发生的错误
new Promise((resolve, reject) => {
// resolve(123);
reject('reason');
})
.finally(data => {
console.log(data);
})
.catch(err => {});
等同于
new Promise((resolve, reject) => {
// resolve(123);
reject('reason');
})
.then(
result => {
return result;
},
err => {
return new Promise((resolve, reject) => {
reject(err);
});
}
)
.then(data => {
console.log(data);
})
.catch(err => {
console.log(err);
});
new Promise(resolve => resolve('foo'));
// 简写
Promise.resolve('foo');
1)一般参数
Promise.resolve('foo').then(data => {
console.log(data);
});
2)特殊参数:Promise 对象
当 Promise.resolve() 接收的是 Promise 对象时,直接返回这个 Promise 对象,什么都不做
const p1 = new Promise(resolve => {
setTimeout(resolve, 1000, '我执行了');
// 等价于
// setTimeout(() => {
// resolve('我执行了');
// }, 1000);
});
Promise.resolve(p1).then(data => {
console.log(data);
});
// 等价于
// p1.then(data => {
// console.log(data);
// });
// console.log(Promise.resolve(p1) === p1); // true
// 当 resolve 函数接收的是 Promise 对象时,后面的 then 会根据传递的 Promise 对象的状态变化决定执行哪一个回调
new Promise(resolve => resolve(p1)).then(data => {
console.log(data);
});
3)具有 then 方法的对象
当参数是具有 then 方法的对象时,会立即执行它的 then 方法
const thenable = {
then(resolve, reject) {
console.log('then');
resolve('data');
// reject('reason');
}
};
Promise.resolve(thenable).then(
data => console.log(data),
err => console.log(err)
);
console.log(Promise.resolve(thenable));
new Promise((resolve, reject) => {
reject('reason');
});
// 简写
Promise.reject('reason');
不管什么参数,都会原封不动地向后传递,作为后续方法的参数
// 以 Promise 对象为例
const p1 = new Promise(resolve => {
setTimeout(resolve, 1000, '我执行了');
});
Promise.reject(p1).catch(err => console.log(err));
new Promise((resolve, rejcet) => {
resolve(123);
})
.then(data => {
// return data;
// return Promise.resolve(data);
return Promise.reject('reason');
})
.then(data => {
console.log(data);
})
.catch(err => console.log(err));
const delay = ms => {
return new Promise(resolve => {
setTimeout(resolve, ms);
});
};
const p1 = delay(1000).then(() => {
console.log('p1 完成了');
// return 'p1';
return Promise.reject('reason');
});
const p2 = delay(2000).then(() => {
console.log('p2 完成了');
return 'p2';
// return Promise.reject('reason');
});
const p = Promise.all([p1, p2]);
p.then(
data => {
console.log(data);
},
err => {
console.log(err);
}
);
const delay = ms => {
return new Promise(resolve => {
setTimeout(resolve, ms);
});
};
const p1 = delay(1000).then(() => {
console.log('p1 完成了');
return 'p1';
// return Promise.reject('reason');
});
const p2 = delay(2000).then(() => {
console.log('p2 完成了');
// return 'p2';
return Promise.reject('reason');
});
const racePromise = Promise.race([p1, p2]);
racePromise.then(
data => {
console.log(data);
},
err => {
console.log(err);
}
);
const delay = ms => {
return new Promise(resolve => {
setTimeout(resolve, ms);
});
};
const p1 = delay(1000).then(() => {
console.log('p1 完成了');
return 'p1';
// return Promise.reject('reason');
});
const p2 = delay(2000).then(() => {
console.log('p2 完成了');
// return 'p2';
return Promise.reject('reason');
});
const allSettledPromise = Promise.allSettled([p1, p2]);
allSettledPromise.then(data => {
console.log('succ', data);
});
new Promise((resolve, reject) => {
// return resolve(123);
return reject('reason');
console.log('hi');
});
Promise.all([1, 2, 3]).then(datas => {
console.log(datas);
});
// 等价于
Promise.all([
Promise.resolve(1),
Promise.resolve(2),
Promise.resolve(3)
]).then(datas => {
console.log(datas);
});
不只是数组,任何可遍历的都可以作为参数
数组、字符串、Set、Map、NodeList、arguments
// 以 Set 为例
Promise.all(new Set([1, 2, 3])).then(datas => {
console.log(datas);
});
const delay = ms => {
return new Promise(resolve => {
setTimeout(resolve, ms);
});
};
const p1 = delay(1000).then(() => {
console.log('p1 完成了');
// return 'p1';
return Promise.reject('reason');
});
// .catch(err => {
// console.log('p1', err);
// });
const p2 = delay(2000).then(() => {
console.log('p2 完成了');
return 'p2';
// return Promise.reject('reason');
});
// .catch(err => {
// console.log('p2', err);
// });
const allPromise = Promise.all([p1, p2]);
allPromise
.then(datas => {
console.log(datas);
})
.catch(err => console.log(err));
异步加载图片
<body>
<img
src="https://img.mukewang.com/5e6af63d00011da318720764.jpg"
alt=""
id="img"
/>
<script>
// 异步加载图片
const loadImgAsync = url => {
return new Promise((resolve, reject) => {
const img = new Image();
img.onload = () => {
resolve(img);
};
img.onerror = () => {
reject(new Error(`Could not load image at ${url}`));
};
img.src = url;
});
};
const imgDOM = document.getElementById('img');
loadImgAsync('https://2img.mukewang.com/5f057a6a0001f4f918720764.jpg')
.then(img => {
console.log(img.src);
setTimeout(() => {
imgDOM.src = img.src;
}, 1000);
})
.catch(err => {
console.log(err);
});
script>
body>