我正在使用 antd 通知组件,如下所示
import { notification } from "antd";
const Notification = ({ msg, type, clearMsg }) => {
return (
<div>
{notification[type]({
description: msg,
})}
{clearMsg()}
</div>
);
};
export default Notification;
Run Code Online (Sandbox Code Playgroud)
我只是在需要弹出通知的任何地方使用它。例如API响应失败后:
const onSubmit = async (e) => {
try {
const res = await login(data);
if (res.message) {
setError({ state: true, msg: res.message });
}
} catch (err) {
console.log(err);
}
};
Run Code Online (Sandbox Code Playgroud)
然后根据错误状态,我在返回正文中显示通知,如下所示:
{ error.state ?
<Notification
type="error"
msg="some msg"
clearMsg={() => setError({state: false, msg: ""})} : …Run Code Online (Sandbox Code Playgroud) 我很难理解与处理 Promise 的情况有何for...of不同。.forEach()
使用这个片段:
const allSettled = async arr => {
const data = []
for (const promise of arr) {
try {
const result = await promise;
data.push(result);
} catch (e) {
data.push(e)
}
}
return data
}
Run Code Online (Sandbox Code Playgroud)
我检查数组中的每个承诺,等待它解决并将结果推送到data. 它按顺序执行。
如果我有这个片段:
const badAllSettled = arr => {
const data = []
arr.forEach(async promise => {
try {
const result = await promise;
data.push(result);
} catch (e) {
data.push(e)
}
})
return data
}
Run Code Online (Sandbox Code Playgroud)
我得到空数组(因为 …