1
function process () {
 return new Promise((resolve, reject) => {


    //make sure module exists
    let module = modules[queueData.module];
    if (!module) reject('module not found: '+queueData.module);

    //make sure processor exists
    let processor = fileProcessors[module.type];
    if (!processor) reject('processor not found: '+module.type);

      return anotherPromiseFunction();
})
}

processFile().catch(e => console.error)

anotherPromiseFunction() returns a promise. normally inside a .then() I can return a promise to make the then() wait for that promise to finish, but how do I do it when creating a promise?

Am I supposed to do this:

    anotherPromiseFunction()
        .then(e=>resolve(e))
        .catch(e=>reject(e))

That seems wrong...

stackers
  • 2,701
  • 4
  • 34
  • 66
  • 1
    Why does that "seem wrong"? Do you have a concrete example which doesn't work as expected? – David Aug 25 '21 at 21:09
  • 1
    I guess just because i'm getting the results of it by hand rather than just forwarding the whole promise upwards.... but if this is how you're supposed to do it then I guess it's okay – stackers Aug 25 '21 at 21:14
  • I don't understand this code. In the first snippet, what's the point of the Promise wrapper? You already have a promise, so there's no gain in wrapping it again. In the second snippet, where is `resolve` coming from? Presumably another promise wrapper, but then we're back to the first problem.... Why the extra wrapper that does nothing? Just return the promise without the wrapper as you say. See [What is the explicit promise construction antipattern and how do I avoid it?](https://stackoverflow.com/questions/23803743/what-is-the-explicit-promise-construction-antipattern-and-how-do-i-avoid-it) – ggorlen Aug 25 '21 at 21:23
  • @ggorlen I guess I oversimplified it (i now edited more back in). The function expects a promise to be returned, but there are a few other things it needs to check before running anotherPromiseFunction(), which need to reject the promise. – stackers Aug 25 '21 at 21:28

1 Answers1

2

You likely do not need the new Promise. The cases for "module exists" and "processor exists" can be handled separately, and then you can just return the call to anotherPromiseFunction after them:

//make sure module exists
let module = modules[queueData.module];
if (!module) {
  return Promise.reject(new Error('module not found: '+queueData.module));
}

//make sure processor exists
let processor = fileProcessors[module.type];
if (!processor) {
  return Promise.reject(new Error('processor not found: '+module.type));
}

return anotherPromiseFunction();

If the enclosing function is an async function, you can just throw the errors instead:

async function run() {
  //make sure module exists
  let module = modules[queueData.module];
  if (!module) {
    throw new Error('module not found: '+queueData.module);
  }

  //make sure processor exists
  let processor = fileProcessors[module.type];
  if (!processor) {
    throw new Error('processor not found: '+module.type);
  }

  return anotherPromiseFunction();
}
sdgluck
  • 24,894
  • 8
  • 75
  • 90