I have an old publishComposite fn that downloads info from an API and then upserts it into a Collection.
I acknowledge that making API callouts in a publication is probably not recommended. The upsert code has been refactored to use bulkOperation and rawCollection. As a result, it’s using async/await.
My question is if it’s ok to use an async fn inside a publishComposite.
Meteor.publishComposite('records', function (recordNum) {
//DL from network
const record = Meteor.call("getRecordsFromAPI", recordNum);
//IIFE to then upsert
(async () => {
const recordUpsert = await addRecordsFromApi(record, this.userId);
})();
}
Thanks!
Update: It looks like the async code does not get waited on. Code following it that returns a cursor gets called first.
You’re welcome! I don’t see any difference between the two versions with respect to their functionality, so I guess it’s more a question of personal preference.
The first one is a bit more verbose; however, it is likely to draw some attention to the fact that a Promise is in play.
The second one is less verbose, but it requires more attention from the reader of the code to realize what’s going on.
It is worth noting that these are purely Meteor specific extensions of Promise, and thus will work only in a Meteor app; therefore any code snippet that leverages these extensions can’t simply be reused in a, say, ordinary Node.js application.
Finally, and just to state the obvious, the great benefit of these Promise extensions is that they provide the equivalence of an await without having to declare the surrounding function async.