gpt4 book ai didi

javascript - 弃用警告 : Calling an asynchronous function without callback is deprecated with AWS JS SDK

转载 作者:搜寻专家 更新时间:2023-11-01 00:29:03 25 4
gpt4 key购买 nike

我第一次尝试使用 Promises 和 AWS JS SDK,但出现以下错误

DeprecationWarning: Calling an asynchronous function without callback is deprecated.

我在下面提供了堆栈跟踪。看来错误发生在我尝试使用 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 方法正确返回 Promise?

最佳答案

如果你想使用 fs.unlink 的版本来返回一个 promise 而不是接受回调,那么使用 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);
}

现在,回答你的问题:

How do I correctly return a Promise from my removeBatch method?

对于 .unlink()mz/fs 版本,它是一个单行代码:

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

关于javascript - 弃用警告 : Calling an asynchronous function without callback is deprecated with AWS JS SDK,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45036503/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com