简体   繁体   English

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

[英]What is the easiest way to wrap Promise in ES6?

I'm using a promise-based package (Axios) for making HTTP requests. 我正在使用基于承诺的程序包(Axios)进行HTTP请求。 So, I have a code like this: 所以,我有这样的代码:

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

I want to write a simple wrapper which generates and sends all the requests, but still has the same syntax. 我想编写一个简单的包装器,该包装器会生成并发送所有请求,但仍具有相同的语法。 It will make the code above look like: 它将使上面的代码如下所示:

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

How can I do this? 我怎样才能做到这一点?

Promises are simple values and can be return ed from functions like everything else. Promise是简单的值,可以像其他所有函数一样从函数中return You seem to be looking for 您似乎在寻找

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM