promise.then 的调用方法
时间: 2023-10-21 15:04:37 浏览: 46
promise.then() 方法可以用来注册一个成功回调函数和一个失败回调函数。该方法接受两个参数,第一个参数是成功回调函数,第二个参数是失败回调函数。
成功回调函数会在 Promise 对象的状态变为 resolved(已解决)时被调用,该函数会接收 Promise 对象传递给它的值作为参数。
失败回调函数会在 Promise 对象的状态变为 rejected(已拒绝)时被调用,该函数会接收 Promise 对象传递给它的拒因作为参数。
以下是 promise.then() 方法的调用方法示例:
```
promise.then(onResolved, onRejected);
```
其中,onResolved 是成功回调函数,onRejected 是失败回调函数。它们都是可选的,你可以只传递一个参数。如果 Promise 对象的状态变为 resolved,则会调用 onResolved 函数;如果 Promise 对象的状态变为 rejected,则会调用 onRejected 函数。
相关问题
手写Promise.then方法
下面是手写一个简单的 Promise.then() 方法的实现方式:
```javascript
function MyPromise(executor) {
this.status = 'pending';
this.value = undefined;
this.onResolvedCallbacks = [];
const resolve = (value) => {
if (this.status === 'pending') {
this.status = 'fulfilled';
this.value = value;
this.onResolvedCallbacks.forEach((callback) => {
callback(this.value);
});
}
};
executor(resolve);
}
MyPromise.prototype.then = function (onResolved) {
return new MyPromise((resolve) => {
if (this.status === 'fulfilled') {
// 如果状态已经是 fulfilled,则直接执行回调函数
resolve(onResolved(this.value));
} else {
// 如果状态还是 pending,则将回调函数加入到待执行列表中
this.onResolvedCallbacks.push((value) => {
resolve(onResolved(value));
});
}
});
};
// 使用示例
const p = new MyPromise((resolve) => {
setTimeout(() => {
resolve('Hello, World!');
}, 2000);
});
p.then((value) => {
console.log(value); // 输出: 'Hello, World!'
}).then(() => {
console.log('Done');
});
```
在上述代码中,我们定义了一个 `MyPromise` 构造函数,它接受一个执行器函数 `executor`,并创建一个新的 Promise 对象。在 `MyPromise` 的原型上定义了 `then` 方法,用于添加回调函数。
在 `then` 方法中,我们首先判断当前 Promise 的状态。如果状态是已完成 (`fulfilled`),则直接执行传入的回调函数 `onResolved`,并使用 `resolve` 方法创建一个新的 Promise 对象,并将回调函数的返回值作为新 Promise 的值传递。
如果状态仍然是待定 (`pending`),则将回调函数加入到待执行列表 `onResolvedCallbacks` 中。当 Promise 的状态变为已完成时,会依次执行待执行列表中的回调函数,并传递相应的值。
需要注意的是,上述实现只是 Promise.then() 方法的简化版本,并没有考虑到其他情况(如错误处理、链式调用等)。完整的 Promise 实现还包括对这些情况的处理。
Promise.allSettled 不执行.then
你好!对于你提到的问题,可能有几个原因导致 `Promise.allSettled` 不执行 `.then` 方法:
1. Promise 链中有错误:如果 `Promise.allSettled` 之前的 Promise 链中存在错误,那么 `.then` 方法将不会执行。你可以使用 `.catch` 方法捕获这些错误并进行处理。
2. `Promise.allSettled` 返回的 Promise 被拒绝:如果 `Promise.allSettled` 返回的 Promise 被拒绝,那么 `.then` 方法将不会执行。你可以通过在 `.then` 方法后添加 `.catch` 方法来处理拒绝情况。
3. `Promise.allSettled` 之前的 Promise 被拒绝:如果 `Promise.allSettled` 之前的 Promise 被拒绝,那么 `Promise.allSettled` 将会立即返回一个被拒绝的 Promise,`.then` 方法将不会执行。同样,你可以通过在 `.then` 方法后添加 `.catch` 方法来处理拒绝情况。
请确保在调用 `.then` 方法之前,以及在整个 Promise 链中适当地处理错误和拒绝情况。希望这些信息对你有帮助!如果还有其他问题,请随时提问。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)