0

I have some sample code I'm trying to run, and I want some asynchronous function that is executing to be called synchronously. I know you need to add async to the function in order to have an await. I've already done that. Still though I get the following error:

  let result = await promise;
               ^^^^^

SyntaxError: await is only valid in async function
    at new Script (vm.js:80:7)
    at createScript (vm.js:274:10)
    at Object.runInThisContext (vm.js:326:10)
    at Module._compile (internal/modules/cjs/loader.js:664:28)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)
    at Module.load (internal/modules/cjs/loader.js:600:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:539:12)
    at Function.Module._load (internal/modules/cjs/loader.js:531:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:754:12)
    at startup (internal/bootstrap/node.js:283:19)

For context here is my code:


async function main() {
  var client = Client.fromConnectionString(deviceConnectionString, Protocol);
  fs.stat(filePath, function (err, fileStats) {
  var fileStream = fs.createReadStream(filePath);

  for (var i=0;i<10;i++) {

    let promise = new Promise((res, rej) => {
      client.uploadToBlob('testblob.txt', fileStream, fileStats.size, function (err, result) {
        if (err) {
          console.error('error uploading file: ' + err.constructor.name + ': ' + err.message);
        } else {
          console.log('Upload successful - ' + result);
        }
        res(i);
      });
    })
    let result = await promise;
    console.log(result);
  }

  fileStream.destroy();
  });
}

main();

Why am I getting said error when my function is already async?

3
  • 5
    fs.stat(filePath, function (err, fileStats) { this function also needs to be async. Commented Aug 9, 2019 at 1:07
  • 5
    fs.stat(filePath, function (err, fileStats) { – everything is inside this callback function, which is not an async function… even if you unindent the code so it looks like it’s not in that function. Use let fileStats = await fs.promises.stat(filePath). Commented Aug 9, 2019 at 1:07
  • 1
    @MichaelBianconi: Putting async on a callback function is generally incorrect. Commented Aug 9, 2019 at 1:07

2 Answers 2

3

You are trying to use async-await inside a function that is not marked with async (your callback).

Just make your callback an async-await function.

async function main() {
  var client = Client.fromConnectionString(deviceConnectionString, Protocol);
  fs.stat(filePath, async function (err, fileStats) {
  var fileStream = fs.createReadStream(filePath);

  for (var i=0;i<10;i++) {

    let promise = new Promise((res, rej) => {
      client.uploadToBlob('testblob.txt', fileStream, fileStats.size, function (err, result) {
        if (err) {
          console.error('error uploading file: ' + err.constructor.name + ': ' + err.message);
        } else {
          console.log('Upload successful - ' + result);
        }
        res(i);
      });
    })
    let result = await promise;
    console.log(result);
  }

  fileStream.destroy();
  });
}

main();
Sign up to request clarification or add additional context in comments.

Comments

0

let result = await promise; the above statement reside within new Promise callback handler which is again a anonymous function.

So, one has to put async against with this function as well as it associated with new scope which is nothing a new promise object.

let promise = new Promise(async (res, rej) => {

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.