title: What is the proper way to combine synchronous code and promises?
body: <p>I am writing a function that serves as an API intermediary function between an actual server API call, and userland.
  This function validates some input data (sync), transforms it as appropriate(sync), reads some other data (async). The results are then merged and used to finally make the actual call to a server endpoint (async).
  This function must always return a promise, and all execution flow in higher-level APIs using it should be handled through promise chaining.</p>
  
  <p>However, I am having trouble figuring out if I'm doing it the proper way. And also, I have some related questions that I cannot find an answer to.</p>
  
  <p>This is the code I wrote up until now:</p>
  
  <pre><code>import axios from "axios";
  import * as MyAPI from "./MyAPI";
  import querystring from "querystring";
  
  function fetchFromServerEndpoint(route, routeParams, optQueryParams) {
      // Is it ok to use Promise.all() to combine sync and async code as below?
      return Promise.all([
          new Promise((resolve, reject) =&gt; {
              const routeReplacedParams = route.replace(/{([^{/]*?)}/g, (match, paramName) =&gt; {
                  const paramValue = routeParams[paramName];
  
                  if (paramValue === undefined) {
                      // Here I need to terminate execution of the replacer function
                      // and reject this promise
                      // Is it ok to `throw` here?
                      throw "UNDEFINED_ROUTE_PARAMETER";
  
                      // Can I somehow `return reject(...)` here instead of throw-ing?
                  }
  
                  return paramValue;
              });
  
              return resolve(routeReplacedParams);
          }),
          // Is it fine to use async to mock a promise with synchronous code?
          async function(){
              return querystring.stringify(optQueryParams);
          }),
          // This is part of a custom API, returns a legit promise
          MyAPI.getAuthorizationAsync(),
      ]).then(([finalRoute, queryParams, authToken]) =&gt; {
          // axios library, all APIs will return a promise
          return axios.get(`${finalRoute}?${queryParams}`, {
              headers: {
                  Authorization: `Basic ${authToken}`
              }
          });
      });
  }
  </code></pre>
  
  <p>My questions are:</p>
  
  <ol>
  <li>What is a proper way to stop execution of a block of code when an error situation arises, from a syncrhonous function, inside a promise? Moreover, like in the replacer function of <code>String.replace</code>, is it ok to 'throw' there? Can you suggest any better option, or maybe, a more promise-friendly approach?  </li>
  <li>Marking a function with <code>async</code> will implictly create a promise, and will also implictly convert (uncaught) <code>throw</code>s into <code>Promise.reject()</code>s and returns intro <code>Promise.resolve()</code>s. The benefit is that you can write code identically to a normal synchronous function, but still have it behave like a promise, so you can chain promises to it. Is this understanding of mine incorrect? Are there drawbacks (or benefits, other than what I just described) that I should be aware of?  </li>
  <li>I used Promise.all() in order to combine the results of multiple, independent stages of the function execution. I suppose this is fine, but are there any other ways to deal with this situation? Or maybe it's a bad practice? Should I, instead, chain promises, and pass-through results from a promise to the next until, I reach the level where I need to use all of them?</li>
  </ol>