在 ES6 中包装承诺的最简单方法是什么?

What is the easiest way to wrap Promise in ES6?

本文关键字:最简单 方法 是什么 承诺 ES6 包装      更新时间:2023-09-26

我正在使用基于承诺的包(Axios(来发出HTTP请求。所以,我有这样的代码:

axios.all(/*many generated requests*/).then((res) => {
      //success handler
    }).catch((err) => {
      //error handler
    });

我想编写一个简单的包装器,它生成并发送所有请求,但仍然具有相同的语法。它将使上面的代码看起来像:

manyReqsWrapper(args).then((res) => {
      //success handler
    }).catch((err) => {
      //error handler
    });

我该怎么做?

承诺是简单的值,可以像其他所有函数一样从函数中return。你似乎在寻找

function mayReqsWrapper(args) {
    return axios.all(/* whatever you need */);
}