gpt4 book ai didi

javascript - 如何用async/await重构这个函数?

转载 作者:搜寻专家 更新时间:2023-10-31 23:03:24 25 4
gpt4 key购买 nike

我对 async/await 很陌生,想知道用 async/await 重构以下代码的最佳方法是什么?

export const createUser = (values, history) => {
return dispatch => {
axios.post('/api/signup', values)
.then(res => {
console.log('result', res);
}, rej => {
console.log('rejection', rej);
});
}
}

当只向 .then 提供一个参数时,这对我来说非常简单,但如果您像这里那样有两个参数会怎样?

最佳答案

这是使用 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function 的方法作为引用:

 const f = (values, history) => {
return async function createUser( dispatch ) {
try {
const res = await axios.post('/api/signup', values);
console.log('result', res);
} catch(e) {
console.log('reject', e);
}
};
}

关于javascript - 如何用async/await重构这个函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46760295/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com