简体   繁体   中英

What is the easiest way to wrap Promise in ES6?

I'm using a promise-based package (Axios) for making HTTP requests. 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. You seem to be looking for

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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