Skip to content

Instantly share code, notes, and snippets.

@yeungon
Created March 30, 2022 04:06
Show Gist options
  • Save yeungon/314fe45756e9c96f29cfb94d66ca19f5 to your computer and use it in GitHub Desktop.
Save yeungon/314fe45756e9c96f29cfb94d66ca19f5 to your computer and use it in GitHub Desktop.
download binary file using Axios
async function getAudioFileFromEconomist(fileUrl) {
const writer = fs.createWriteStream("economist_audio.mp3");
return axios({
method: "get",
url: fileUrl,
responseType: "stream",
}).then((response) => {
return new Promise((resolve, reject) => {
response.data.pipe(writer);
let error = null;
writer.on("error", (err) => {
error = err;
writer.close();
reject(err);
});
writer.on("close", () => {
if (!error) {
resolve(true);
}
});
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment