【问题标题】:NodeJS, request-promises are not waiting for each otherNodeJS,请求承诺不会互相等待
【发布时间】:2017-10-24 14:50:21
【问题描述】:

所以我将我的问题分解为一个简单的代码 sn-p。

我希望otherRequestes 等待我的firstRequests,但不知何故这不起作用。 firstRequested 永远不会等待

const rp = require('request-promise');

const firstRequest = () => {
  return rp('http://www.google.com')
    .then(function(htmlString) {
      console.log('in then of firstrequest');
    })
    .catch(function(err) {
      console.log('catch', err);
    });
}

laterRequest = (i) => {
  return rp('http://www.google.com')
    .then(function(htmlString) {
      console.log('in then' + i);
    })
    .catch(function(err) {
      console.log('catch', err);
    });

}

const requests = [];

for (let i = 0; i < 10; i += 1) {
  requests.push(laterRequest(i));
}

firstRequest().then(() => {
  Promise.all(requests).then(() => {
    console.log('all promises returned');
  });
});

所以我想要的输出是“in then of firstrequest”之后的输出应该是后来的请求,我不在乎他们的顺序。

但是当我当前运行它时,我的输出如下,firstRequest 在输出中随机结束:

in then0
in then5
in then2
in then3
in then1
in then8
in then4
in then6
in then9
in then7
in then of firstrequest
all promises returned

【问题讨论】:

  • 问题是,您与 firstRequest 并行启动其他 10 个请求 - 您需要在 firstRequest 中使用 for 循环。然后

标签: javascript node.js promise request


【解决方案1】:

当您执行for 循环时,您正在调用所有其他承诺之前您已经调用了第一个。调用你的第一个 Promise,然后在 .then()(当你知道它完成时)开始调用其他 Promise。

firstRequest().then(() => {
  const requests = [];

  for (let i = 0; i < 10; i += 1) {
    requests.push(laterRequest(i));
  }

  return Promise.all(requests).then(() => {
    console.log('all promises returned');
  });
});

【讨论】:

    【解决方案2】:

    您的firstRequest 没有返回承诺。您已经通过在函数中将.then 附加到它来附加一个解析处理程序。

    对于如何构建此代码,您有几个不同的选项,但看起来您希望在每个承诺成功或失败时将一些内容记录到控制台,以便您可以执行以下操作:

    const firstRequest = () => {
        return new Promise((resolve, reject) => { // actually returns a promise now
            rp('http://www.google.com')
                .then(function(htmlString) {
                    console.log('in then of firstrequest');
                    resolve(htmlString); // promise resolves (i.e. calles its `.then` function)
                })
                .catch(function(err) {
                    console.log('catch', err);
                    reject(err); // promise rejects (calles the `.catch`)
                });
        })
    }
    
    const laterRequest = (i) => {
        return new Promise((resolve, reject) => {
            rp('http://www.google.com')
                .then(function(htmlString) {
                    console.log('in then' + i);
                    resolve(htmlString);
                })
                .catch(function(err) {
                    console.log('catch', err);
                    reject(err);
                });
        })
    }
    
    const requests = [];
    
    for (let i = 0; i < 10; i += 1) {
        requests.push(laterRequest(i));
    }
    
    firstRequest().then(() => {
        Promise.all(requests).then(() => {
            console.log('all promises returned');
        });
    });
    

    简短回答

    您也可以跳过重复 thens 和 catches 并直接返回 rp 的承诺:

    const firstRequest = () => rp('http://www.google.com');
    
    const laterRequest = (i) => rp('http://www.google.com');
    
    
    firstRequest().then(() => {
    
        const requests = [];
        for (let i = 0; i < 10; i += 1) {
            requests.push(laterRequest(i));
        }
    
        Promise.all(requests).then(() => {
            console.log('all promises returned');
        });
    });
    

    【讨论】:

    • 但这仍然不会改变执行顺序,我运行了你的两个示例,得到的结果与上面相同。 firstrequest 最后执行。
    • @acid 刚刚编辑了我的简短示例。 firstRequest 最后执行,因为您实际上是在运行 firstRequest 之前运行其他函数。我在我的编辑中改变了这个。立即尝试。
    猜你喜欢
    • 1970-01-01
    • 2017-10-20
    • 2021-10-10
    • 2021-11-26
    • 2020-05-17
    • 2019-04-29
    • 1970-01-01
    • 2022-11-26
    • 1970-01-01
    相关资源
    最近更新 更多