js .finally
时间: 2023-11-26 18:46:00 浏览: 42
.finally()是Promise对象的一个方法,无论Promise对象最后的状态如何,都会执行finally方法指定的回调函数。通常用于在Promise结束时进行一些清理工作,比如关闭文件流、释放内存等。在上述代码中,当Promise对象执行完.then()或.catch()指定的回调函数后,会执行.finally()指定的回调函数,将loading状态置为false。
相关问题
Promise .finally
Promise.finally()方法用于指定不管Promise对象最后状态如何,都会执行的操作。finally()方法返回一个Promise对象,当Promise结束时,无论结果是fulfilled或者是rejected,都会执行指定的回调函数。这为在Promise是否成功完成后都需要执行的代码提供了一种方式。
以下是一个使用Promise.finally()的例子:
```javascript
function fetchData() {
return fetch('https://jsonplaceholder.typicode.com/todos/1')
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error(error))
.finally(() => console.log('fetchData()执行完毕'))
}
fetchData();
```
在上面的例子中,无论fetch()方法返回的Promise对象是成功还是失败,finally()方法中的代码都会被执行。在这个例子中,我们使用了fetch()方法来获取一个JSON数据,并在控制台中打印出来。如果fetch()方法返回的Promise对象成功,我们会在then()方法中打印数据,否则我们会在catch()方法中打印错误信息。最后,我们使用finally()方法来打印一条消息,表示fetchData()方法已经执行完毕。
模拟实现一个 Promise.finally
好的,我可以帮你模拟实现一个 Promise.finally 方法。这个方法会在 Promise 完成或被拒绝后都会执行一段回调函数。
首先,我们需要创建一个 Promise 类,它包含一个状态和相关的方法:
```javascript
class MyPromise {
constructor(executor) {
this.state = 'pending';
this.value = undefined;
this.error = undefined;
this.finallyCallback = undefined;
const resolve = value => {
if (this.state === 'pending') {
this.state = 'fulfilled';
this.value = value;
if (this.finallyCallback) {
this.finallyCallback();
}
}
};
const reject = error => {
if (this.state === 'pending') {
this.state = 'rejected';
this.error = error;
if (this.finallyCallback) {
this.finallyCallback();
}
}
};
try {
executor(resolve, reject);
} catch (error) {
reject(error);
}
}
finally(callback) {
if (this.state !== 'pending') {
callback();
} else {
this.finallyCallback = callback;
}
}
}
```
在这个实现中,我们使用了一个 `finallyCallback` 属性来保存传入的回调函数。当 Promise 的状态变为 `fulfilled` 或 `rejected` 时,如果有回调函数存在,就会执行它。
接下来,我们可以使用这个自定义的 Promise 类来测试实现的 `finally` 方法:
```javascript
const promise = new MyPromise((resolve, reject) => {
setTimeout(() => {
resolve('Success');
}, 2000);
});
promise
.then(value => {
console.log(value);
})
.finally(() => {
console.log('Finally callback executed');
});
```
这段代码创建了一个 Promise,经过2秒后状态变为 `fulfilled`,然后会执行 `then` 方法中的回调函数,并在最后执行 `finally` 方法中的回调函数。
希望这个简单的实现能帮到你!
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![md](https://img-home.csdnimg.cn/images/20210720083646.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)