[英]Add the result of a chained promise into promise.all()
I try construct a promise, named downloadTextPromise
, to download a file and then in Promise.all()
to read the file content. 我尝试构造一个名为
downloadTextPromise
的诺言,以downloadTextPromise
文件,然后在Promise.all()
中读取文件内容。 However, I still cannot read the file content in Promise.all().then
. 但是,我仍然无法读取
Promise.all().then
的文件内容。
I suspect that downloadTextPromise
is a two-step promise, so only the first step (HTTP GET) is pushed into promises array. 我怀疑
downloadTextPromise
是一个两步承诺,因此只有第一步(HTTP GET)被推送到promises数组中。 But I have little idea how to fix it. 但是我不知道如何解决它。
Any hint? 有什么提示吗? any solution that works is welcoming,
request-promise-native
package is not compulsory. 任何可行的解决方案都是欢迎的,基于
request-promise-native
包不是强制性的。
My code is as below: 我的代码如下:
const request = require("request-promise-native");
let promises = [];
//put several promises into promises array;
.....
let downloadTextPromise = request.get("http://example.com/xyz.txt").pipe(fs.createWriteStream(`~/xyz.txt`));
promises.push(downloadTextPromise);
Promise.all(promises).then(() => {
//I need xyz.txt content at this stage, but it is not ready immediately.
}
Firstly, check if downloadTextPromise
is actually a promise. 首先,检查
downloadTextPromise
是否确实是一个承诺。 The library's documentation doesn't mention how pipe
works. 该库的文档没有提及
pipe
工作方式。 If it returns a native promise, you can do console.log(downloadTextPromise instanceof Promise)
. 如果返回本地承诺,则可以执行
console.log(downloadTextPromise instanceof Promise)
。 Otherwise, you can try console.log(!!downloadTextPromise.then)
. 否则,您可以尝试
console.log(!!downloadTextPromise.then)
。
If it's a promise, then one possibility is that the promise is resolved synchronously after the the stream ends. 如果这是一个承诺,则一种可能性是在流结束后同步解决该承诺。 The code to actually write the file is added to the message queue.
实际写入文件的代码已添加到消息队列中。 In this case, you can fix it by doing:
在这种情况下,您可以通过以下方法解决此问题:
Promise.all(promises).then(() => {
setTimeout(() => {
// Do stuff with xyz.txt
}, 0);
}
The issue is the writeStream part, so I should build a function returning correct promise. 问题是writeStream部分,因此我应该构建一个返回正确承诺的函数。
I don't find the way to construct one returning a promise from request.get().pipe()
, instead I use request.get().then()
and fs.writeFile()
as below. 我没有找到一种方法来构造一个从
request.get().pipe()
返回承诺的方法,而是使用如下的request.get().then()
和fs.writeFile()
。
However, Another problem appears from the new solution: This fs.writeFile
works all OK for txt files; 但是,新解决方案中出现了另一个问题:此
fs.writeFile
可以很好地处理txt文件; for mp3 or mp4, the function returns no error, but the file content is invalid for further processing in Promise.all().then()
. 对于mp3或mp4,该函数不返回错误,但文件内容无效,
Promise.all().then()
在Promise.all().then()
进行进一步处理。
function downloadTextPromise() {
return new Promise((resolve, reject) => {
rp.get("http://example.com/xyz.txt").then(data => {
fs.writeFile("./xyz.txt, data, err => {
if (err) reject(err);
resolve(true);
});
});
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.