r/backtickbot • u/backtickbot • Feb 27 '21
https://np.reddit.com/r/node/comments/lte28q/how_can_i_improve_multiple_returns_new_promise/goxvdbv/
Your code and this should be more or less equivalent.
There's no need to wrap promises in another promise. Another thing is that promises can only resolve/reject to a single value, so there's no reject("some error", error) which is why I removed the .then or .catches that you had there.
Lastly, I added a rejection at the end in case any of the above conditions were not matched. If you don't do this, the function will return undefined, and you won't be able to await/.then/.catch it anymore.
if (itemsV.length >= 1 && itemsA.length === 1)
return eFunc
.createJob1(tag, inputs, 0, path)
.then((job) => backend.update(vrID, null, status.IN_PROGRESS));
if (itemsV.length === 1 && itemsA.length > 1)
return eFunc
.createJob2(tag, inputs, 0, path)
.then((job) => backend.update(vrID, null, status.IN_PROGRESS));
if (itemsV.length > 1 && itemsA.length > 1)
return backend.update(vrID, null, status.FAILED).then((result) => "callback");
return Promise.reject("No matches?");
0
Upvotes