JustLogin JustLogin - 18 days ago 7
Javascript Question

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?

Answer

Promises are simple values and can be returned from functions like everything else. You seem to be looking for

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