Search code examples
node.jsfileasync-awaitmv

npm mv module with async/await


is there a way to use the mv npm module in an async/await manner? not with a callback as provided in docs. Or is there any other module that does the same job? thanks

var mv = require('mv');
 
mv('source/file', 'dest/file', function(err) {
  ...
});

Solution

  • You can use the promisify function of the util package

    const util = require('util');
    const mv = require('mv');
    
    const mvPromise = util.promisify(mv);
    
    await mvPromise('source/file', 'dest/file');