Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What to do when there's nothing to return in a promise?

When I have a promise, I usually do something like:

funcPromise()
.then(()=> {
    // some stuff happens
    return value; // what if there's nothing to return here?
})
.then(()=> { //... 
})
.catch(err=>log(err));

But if there's nothing to return, should i do return Promise.resolve() or return null, or simply return;?? I know that in a one-liner, the arrow function has implicit return, but for my case, it's a multi-statement function.

like image 204
frozen Avatar asked Jul 16 '17 02:07

frozen


2 Answers

It doesn't matter.

If you have no return statement (or a return statement with no value), the function will return undefined, resulting in a promise of undefined.

That is presumably fine for you.

like image 129
SLaks Avatar answered Nov 15 '22 03:11

SLaks


The promise actually returns an object that has a value if you call the right method. Maybe that helps?

like image 31
Animatry Avatar answered Nov 15 '22 04:11

Animatry