[英]JavaScript - Passing data to callback with predefined parameters
如果有人可以帮助我,我将非常感激。 我认为可能有一个简单的解决方案,但是我无法解决,这非常令人沮丧。
我正在使用Expo开发一个React Native应用。 他们的SDK具有“ downloadResumable”功能,允许用户下载文件。
提供下载进度信息的回调函数获取一个对象,该对象具有自动传递给它的“ totalBytesWritten”和“ totalBytesExpectedToWrite”道具。
有什么方法可以将我传递给createDownloadResumable的'songId'参数传递给回调函数,从而使回调不需要引用外部的'_id'变量?
预先感谢任何可以帮助我解决这个问题的人!
const { _id } = song;
const callback = ({ totalBytesWritten, totalBytesExpectedToWrite }) => dispatch(updateDownloadProgress(
_id,
totalBytesWritten,
totalBytesExpectedToWrite
));
const createDownloadResumable = songId => FileSystem.createDownloadResumable(
link,
FileSystem.documentDirectory + `${songId}.mp3`,
{},
callback,
);
const downloadResumable = createDownloadResumable(_id);
您应该能够通过在闭包中创建回调来做到这一点,如下所示:
const { _id } = song;
const generateCallback = (songId) => {
return ({ totalBytesWritten, totalBytesExpectedToWrite }) => dispatch(updateDownloadProgress(
songId,
totalBytesWritten,
totalBytesExpectedToWrite
));
}
const createDownloadResumable = (songId) => FileSystem.createDownloadResumable(
link,
FileSystem.documentDirectory + `${songId}.mp3`,
{},
generateCallback(songId),
);
const downloadResumable = createDownloadResumable(_id);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.