As I said your function logic has problems even before adding context.async.
Make it run locally on your machine as a node script, then when it’s working use it in the plugin.
You can find plenty of examples in the forum about how to use context.async.
See here for a recent example.