无法从嵌套的异步调用返回字符串

时间:2019-05-27 12:37:13

标签: javascript node.js asynchronous

场景:

下面是反映2个嵌套异步调用的代码,

const create = (request) => {
    axios.post(link, body)
    .then((response) => {
       arr.forEach(obj => {
            const file = fs.createWriteStream("./downloads/" + obj.name);
            https.get(obj.download_url, response => {
                let stream = response.pipe(file);
                 stream.on("finish", () => {
                    if (i == count) {
                       return "Hi...."
                    }
                    i++;
                });
            });
        });
    })
    .catch((error) => {
        return error;
    });
};

问题:

如何将“ Hi ....”返回给用户。

0 个答案:

没有答案