Common resources - Async Code Doesn't Finish Before Done is Called

When making multiple API calls from within the JavaScript in a Common Resource, promises and thus asynchronous calls are typically used.  To see an example of this, check out the Sugar Integrate Open Labs Common Resource repository (async in a Common Resource).  Care should be taken to avoid calling `done` before the async work is complete.

An example where `done` could be called before the async code is finished: 

if (fromVendor) {
  // Some async work that takes a few seconds to complete
  // ...

  Promise.all(promises).then(() => {
    // Return once the promises are resolved
    done(transformedObject);
  });
}

// JavaScript will not wait for the above promises to resolve
done(transformedObject);

An example where `done` will not be called before the async code is finished:

if (!fromVendor) done(transformedObject);

if (fromVendor) {
  // Some async work that takes a few seconds to complete
  // ...
  Promise.all(promises).then(() => {
    // Return once the promises are resolved
    done(transformedObject);
  });
}