Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/471.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript NodeJS,请求承诺不是在等待对方_Javascript_Node.js_Promise_Request - Fatal编程技术网

Javascript NodeJS,请求承诺不是在等待对方

Javascript NodeJS,请求承诺不是在等待对方,javascript,node.js,promise,request,Javascript,Node.js,Promise,Request,因此,我将我的问题分解为一个简单的代码片段 我希望otherRequestes等待我的firstRequests,但不知何故,这不起作用。从不等待首次请求的 const rp = require('request-promise'); const firstRequest = () => { return rp('http://www.google.com') .then(function(htmlString) { console.log('in then of

因此,我将我的问题分解为一个简单的代码片段

我希望
otherRequestes
等待我的
firstRequests
,但不知何故,这不起作用。从不等待首次请求的

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

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

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');
  });
});
firstRequest()。然后(()=>{
常量请求=[];
对于(设i=0;i<10;i+=1){
请求。推送(请求(i));
}
返回承诺。所有(请求)。然后(()=>{
log(“返回所有承诺”);
});
});

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

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');
  });
});
firstRequest()。然后(()=>{
常量请求=[];
对于(设i=0;i<10;i+=1){
请求。推送(请求(i));
}
返回承诺。所有(请求)。然后(()=>{
log(“返回所有承诺”);
});
});

您的
第一次请求
不会返回承诺。通过在函数中附加
。然后将
附加到解析处理程序,您已经将解析处理程序附加到了它

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

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

您的
firstRequest
不会返回承诺。通过在函数中附加
。然后将
附加到解析处理程序,您已经将解析处理程序附加到了它

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

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

问题是,您与firstRequest并行启动10个其他请求-您需要firstRequest中的for循环。然后问题是,您与firstRequest并行启动10个其他请求-您需要firstRequest中的for循环。但是这仍然不会改变执行顺序,我运行了你的两个例子,得到了与上面相同的结果。firstrequest将在最后执行。@acid刚刚编辑了我的简短示例
firstRequest
在最后执行,因为在运行
firstRequest
之前,您实际上正在运行其他函数。我在编辑中更改了这个。现在试试,但这仍然不会改变执行顺序,我运行了两个示例,得到了与上面相同的结果。firstrequest将在最后执行。@acid刚刚编辑了我的简短示例
firstRequest
在最后执行,因为在运行
firstRequest
之前,您实际上正在运行其他函数。我在编辑中更改了这个。现在试试看。