[TOC]
>[success] # Promise.allSettled
**Promise.allSettled** 方法與 **Promise.all** 不同點:
1. **Promise.all** : 有一個請求錯誤,直接走 **.catch** ,不再走 **.then** 。
2. **Promise.allSettled** : **無論請求對錯最終都會返回一個數組對象** 到 **.then** 中,并切**返回的數據中標識了錯誤跟正確數據的區別** 。
以上就是 **Promise.allSettled** 方法與 **Promise.all** 的區別,但是 **Promise.allSettled** 是新出方法 **部分瀏覽器不支持** ,如下 **Promise.allSettled** 是第四階段的草案 ,所以想使用這個方法,可以使用 **babel墊片** 或者自己 **手動封裝一個** , 我們要實現的就是后者。

>[success] ## 封裝 allSettled 方法
**allSettled 方法代碼如下**
~~~
let allSettled = (funcArr) => {
return new Promise((resolve) => {
let sttled = 0
let result = []
for(let index = 0;index<funcArr.length;index++){
const element = funcArr[index]
element
.then(res => {
result[index] = {
status: 'fulfilled',
value: res
}
})
.catch(err => {
result[index] = {
status: 'rejected',
reason: err
}
})
.finally(() => { ++sttled === funcArr.length && resolve(result) })
}
})
}
~~~
使用時
~~~
const promises = [
Promise.reject('c'),
Promise.resolve('a'),
Promise.resolve('b'),
];
allSettled(promises).then(res => {
console.log(res)
})
// 打印結果
// [{"status":"rejected","reason":"c"},
// {"status":"fulfilled","value":"a"},
// {"status":"fulfilled","value":"b"}]
~~~
- vue復選框邏輯
- get請求給后臺傳數組
- 提交表單時傳值參數處理方案
- Element ui上傳圖片功能
- async和await的使用
- 時間戳轉換
- 日期格式轉換時間戳
- 時間戳轉換日期格式
- 對深拷貝的認知總結
- vue-右鍵菜單功能
- textarea中換行、回車、空格的識別與處理
- element ui表格合并
- 合并行(上下行)
- 雙層for循環
- 數組去重
- 瀑布流
- 前端多條件篩選
- 閉包的理解
- 改變this指向
- vue單選框邏輯
- 對象數組根據某個屬性來進行排序
- vscode插件整理
- 對象數組多條件去重
- Blob類型數據轉換Json數據類型
- Element ui做批量上傳功能
- 前端cookie和后端cookie
- 強制轉換https協議
- 給事件傳額外參數
- 樹形結構數據處理
- 查找所有父級數據
- 根據id篩選單條數據
- 動態引入阿里圖標庫
- 四舍五入
- 封裝一個Promise.allSettled方法
- 判斷輸入框內是否有emoji表情
- element-ui的popover組件位置偏移
- formData上傳文件時,攜帶【數組對象參數】
- 前端解壓壓縮包(zip)解壓后上傳解壓的文件
- element ui表格列相同解決辦法
- elementUI,table復選框多選,翻頁/切換條數時保持選中狀態
- cookie 和 token 的區別