admin管理员组

文章数量:1405366

When removing async and await the code works normally, what's wrong to get this error if unlink is a async function? And in this case is it really obligatory to have a resolve(...) inside the promise since the unlink only deletes a file function and return null?

c:\Users\Flavio\Documents\Coding\projects-my\study-content\study-luiz-miranda\node\file-system\unlink.js:7
  let deletedFile = await fs.unlink(path.join(dir, file));
                    ^^^^^

SyntaxError: await is only valid in async function
    at wrapSafe (internal/modules/cjs/loader.js:984:16)
    at Module._pile (internal/modules/cjs/loader.js:1032:27)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1097:10)
    at Module.load (internal/modules/cjs/loader.js:933:32)
    at Function.Module._load (internal/modules/cjs/loader.js:774:14)
    at Module.require (internal/modules/cjs/loader.js:957:19)
    at require (internal/modules/cjs/helpers.js:88:18)
    at Object.<anonymous> (c:\Users\Flavio\Documents\Coding\projects-my\study-content\study-luiz-miranda\node\app.js:1:24)
    at Module._pile (internal/modules/cjs/loader.js:1068:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1097:10)
const fs = require('fs').promises;
const path = require('path');

exports.deleteFile = async (dir, file) => new Promise((resolve, reject) => { 
  let deletedFile = await fs.unlink(path.join(dir, file), (err) => {
    if (err) return reject(err);
  });
  resolve(console.log('Deleted'))
})

When removing async and await the code works normally, what's wrong to get this error if unlink is a async function? And in this case is it really obligatory to have a resolve(...) inside the promise since the unlink only deletes a file function and return null?

c:\Users\Flavio\Documents\Coding\projects-my\study-content\study-luiz-miranda\node\file-system\unlink.js:7
  let deletedFile = await fs.unlink(path.join(dir, file));
                    ^^^^^

SyntaxError: await is only valid in async function
    at wrapSafe (internal/modules/cjs/loader.js:984:16)
    at Module._pile (internal/modules/cjs/loader.js:1032:27)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1097:10)
    at Module.load (internal/modules/cjs/loader.js:933:32)
    at Function.Module._load (internal/modules/cjs/loader.js:774:14)
    at Module.require (internal/modules/cjs/loader.js:957:19)
    at require (internal/modules/cjs/helpers.js:88:18)
    at Object.<anonymous> (c:\Users\Flavio\Documents\Coding\projects-my\study-content\study-luiz-miranda\node\app.js:1:24)
    at Module._pile (internal/modules/cjs/loader.js:1068:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1097:10)
const fs = require('fs').promises;
const path = require('path');

exports.deleteFile = async (dir, file) => new Promise((resolve, reject) => { 
  let deletedFile = await fs.unlink(path.join(dir, file), (err) => {
    if (err) return reject(err);
  });
  resolve(console.log('Deleted'))
})
Share Improve this question asked Jun 18, 2021 at 22:00 user113581321user113581321 1372 silver badges8 bronze badges 1
  • 1 It doesn't make sense to promisify unlink. It already returns a promise because you use fs.promises. – Estus Flask Commented Jun 18, 2021 at 22:08
Add a ment  | 

2 Answers 2

Reset to default 3

You're using fs.promises.unlink, which does not take a callback. It instead returns a promise that you can choose to await. You also don't need to use new Promise() - new Promise() is meant to turn a callback-API into a promised-based one, but you're already working with a promise-based API.

So, this is actually all it takes to do what you're trying to do:

const fs = require('fs').promises;
const path = require('path');

exports.deleteFile = async (dir, file) => {
  await fs.unlink(path.join(dir, file));
  console.log('Deleted')
}

To answer your question about weather you're required to use resolve() when using new Promise(), the answer is yes, otherwise your promise will never resolve, and anything waiting for it will await forever. However, you don't have to provide resolve() with any specific value if you don't have one to provide.

you should better understand the promises

const fs = require('fs').promises;
const path = require('path');

exports.deleteFile = async (dir, file) => { 
    await fs.unlink(path.join(dir, file))      
    console.log('Deleted')
}

an async/await function always returns a promise.

本文标签: javascriptfsunlink asyncronous nodejsStack Overflow