Nodejs异步/延迟等待
Nodejs async / await with delay
我对这段代码有疑问:
var request = require('request-promise');
class Test{
constructor(){
}
async Start(){
var response = await this.getResponse();
await console.log(response);
}
async getResponse(){
var options = {
uri: "https://www.google.com"
}
var response = await request(options);
setTimeout(function(){
return response;
},1000);
}
}
module.exports = Test;
当我 运行 Start() 时,控制台记录 "undefined",但这是为什么?我知道我在 return 上设置了 1 秒的延迟,但代码不应该等到 return 吗?因为 await?
P.S:延迟是为了模拟正在处理的响应数据。
你为什么不直接使用 promises:
var request = require('request-promise');
class Test{
constructor(){
}
Start(){
this.getResponse()
.then(response => console.log(response))
.catch(err => console.log(err));
}
getResponse(){
var options = {
uri: "https://www.google.com"
}
return request(options);
}
}
module.exports = Test;
如果你真的想在 1000 之后发送响应,请使用 Promise
否则没有必要这样做。
var request = require('request-promise');
class Test{
constructor(){
}
async Start(){
var response = await this.getResponse();
await console.log(response);
}
async getResponse(){
var options = {
uri: "https://www.google.com"
}
var response = await request(options);
return new Promise((resolve) => {
setTimeout(() => resolve(response), 1000)
})
}
}
module.exports = Test;
您不能将 "return" 放在另一个函数内部并期望它 return 到外部函数。 (最大的问题)
async getResponse(){
setTimeout(function(){
return "Test";
},1000);
return undefined; // line is basically what is here when you don't return anything
}
await getReponse(); // returns undefined, NOT "Test".
您可以改为这样编写代码:
const delay = time => new Promise(res=>setTimeout(res,time));
class Test{
constructor(){
}
async Start(){
var response = await this.getResponse();
console.log(response); // await not needed here.
}
async getResponse(){
var options = {
uri: "https://www.google.com"
}
var response = await request(options);
await delay(1000); // since we're using async functions, we can "await" a promise
return response;
// previous code would return "undefined" when after it called setTimeout
}
}
module.exports = Test;
我对这段代码有疑问:
var request = require('request-promise');
class Test{
constructor(){
}
async Start(){
var response = await this.getResponse();
await console.log(response);
}
async getResponse(){
var options = {
uri: "https://www.google.com"
}
var response = await request(options);
setTimeout(function(){
return response;
},1000);
}
}
module.exports = Test;
当我 运行 Start() 时,控制台记录 "undefined",但这是为什么?我知道我在 return 上设置了 1 秒的延迟,但代码不应该等到 return 吗?因为 await?
P.S:延迟是为了模拟正在处理的响应数据。
你为什么不直接使用 promises:
var request = require('request-promise');
class Test{
constructor(){
}
Start(){
this.getResponse()
.then(response => console.log(response))
.catch(err => console.log(err));
}
getResponse(){
var options = {
uri: "https://www.google.com"
}
return request(options);
}
}
module.exports = Test;
如果你真的想在 1000 之后发送响应,请使用 Promise
否则没有必要这样做。
var request = require('request-promise');
class Test{
constructor(){
}
async Start(){
var response = await this.getResponse();
await console.log(response);
}
async getResponse(){
var options = {
uri: "https://www.google.com"
}
var response = await request(options);
return new Promise((resolve) => {
setTimeout(() => resolve(response), 1000)
})
}
}
module.exports = Test;
您不能将 "return" 放在另一个函数内部并期望它 return 到外部函数。 (最大的问题)
async getResponse(){
setTimeout(function(){
return "Test";
},1000);
return undefined; // line is basically what is here when you don't return anything
}
await getReponse(); // returns undefined, NOT "Test".
您可以改为这样编写代码:
const delay = time => new Promise(res=>setTimeout(res,time));
class Test{
constructor(){
}
async Start(){
var response = await this.getResponse();
console.log(response); // await not needed here.
}
async getResponse(){
var options = {
uri: "https://www.google.com"
}
var response = await request(options);
await delay(1000); // since we're using async functions, we can "await" a promise
return response;
// previous code would return "undefined" when after it called setTimeout
}
}
module.exports = Test;