1

I am using a static Model method and need a function within it to act synchronously. I installed the babel-plugin-transform-async-to-generator npm package, but get the warning:

await is a reserved word

Here is the static method:

SomeSchema.statics.doSomething = async function(data, callback) {
    ...
    this.model('Template').findById(id, function (err, doc) {
        let ref = await getNextSequence();
        ...
    });
    ...
};
JoeTidee
  • 24,754
  • 25
  • 104
  • 149

1 Answers1

2

Needed async on the query callback too:

SomeSchema.statics.doSomething = async function(data, callback) {
    ...
    this.model('Template').findById(id, async function (err, doc) {
        let ref = await getNextSequence();
        ...
    });
    ...
};
JoeTidee
  • 24,754
  • 25
  • 104
  • 149
  • Do you know whether `async` is required on *both* the wrapper function *and* the query callback - or *just* the query callback? I am looking at this answer that says: `In order to use await, the function *directly enclosing it* needs to be async` https://stackoverflow.com/a/42299762 – user1063287 Nov 13 '18 at 09:39