如何使用 async/await 重构此函数?

doc*_*pus 5 javascript node.js async-await

我对 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);
      });
  }
}
Run Code Online (Sandbox Code Playgroud)

当只提供一个参数时,.then对我来说非常简单,但是如果你有两个像这里这样的参数会发生什么?

Dun*_*ker 4

以下是如何做到这一点,使用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);
        }         
    };
 }
Run Code Online (Sandbox Code Playgroud)