DeprecationWarning:不使用callback调用asynchronous函数已被AWS JS SDK弃用

我试图使用Promises和AWS JS SDK第一次,我得到以下错误

DeprecationWarning:不build议调用不带callback的asynchronous函数。

我在下面提供了一个堆栈跟踪。 它似乎错误发生在我尝试使用fs.unlink删除我下载的文件。

 exports.generate = function (req, res) { if (typeof Promise === 'undefined') { AWS.config.setPromisesDependency(require('bluebird')); } var removeBatch = function removeBatch(files) { return Promise.all(files.map(function(file) { return fs.unlink(file.key); })); }; var getBatch = function getBatch(files) { return Promise.all(files.map(function(file) { var params = { Bucket: 'my-bucket', Key: file.key }; return app.s3.getObject(params).createReadStream().pipe(file.stream); })); }; var fileNames = ['Original 106fm Logo #268390.jpg', 'test.jpg']; var files = fileNames.map(function(fileName) { return { key: fileName, stream: fs.createWriteStream(fileName) }; }); getBatch(files) .then(removeBatch.bind(null, files)) .catch(console.error.bind(console)); } 

这是堆栈跟踪

 (node:63311) [DEP0013] DeprecationWarning: Calling an asynchronous function without callback is deprecated. at makeCallback (fs.js:127:12) at Object.fs.unlink (fs.js:1054:14) at /src/splash.js:12:7 at Array.map (native) at removeBatch (/src/splash.js:11:28) at <anonymous> at process._tickDomainCallback (internal/process/next_tick.js:208:7) 

我如何正确地从我的removeBatch方法返回一个承诺?

如果你想使用一个fs.unlink版本来返回一个promise,而不是callback,那么使用这个mz模块:

 const fs = require('mz/fs'); 

看文档:

它不仅会让你做这样的事情:

 fs.unlink(name) .then(() => console.log('Success')) .catch(err => console.log('Error:', err)); 

而且这里面的async函数:

 try { await fs.unlink(name); } catch (e) { console.log('Error:', e); } 

现在,对你的问题:

我如何正确地从我的removeBatch方法返回一个承诺?

使用.unlink()mz/fs版本是一个单行的:

 const removeBatch = files => Promise.all(files.map(file => fs.unlink(file.key));