NodeJS,请求 - 承诺不等待彼此

时间:2017-05-24 10:10:43

标签: javascript node.js promise request

所以我把我的问题打破了一个简单的代码片段。

我希望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');
  });
});

所以我想要的输出是#34;然后是第一次请求&#34;在那之后,输出应该是后来的请求,我不关心他们的订单。

但是当我运行它时,我的输出如下,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

2 个答案:

答案 0 :(得分:1)

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

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');
  });
});

答案 1 :(得分:1)

您的firstRequest未返回承诺。您已经通过在函数中附加.then来附加解析处理程序。

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

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');
    });
});

更短的回答

你也可以跳过重复thencatch es并直接返回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');
    });
});