Nodejs async / await with delay
Asked Answered
A

3

8

I have a problem with this code:

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;

When I run the Start(), the console logs "undefined", but why is this? I know that I set a 1 second delay on the return, but shouldn't the code wait until the return? because of the await?

P.S: The delay is to simulate the response data being processed.

Apoplexy answered 27/8, 2018 at 10:58 Comment(2)
Why do you need to wait 1 second and what does the request() method do?Superstition
@SvetoslavPetrov It is to simulate the body response being processedApoplexy
E
7

Use Promise for this if you really want to send response after 1000 otherwise there is no need for doing this.

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;
Endsley answered 27/8, 2018 at 11:14 Comment(0)
C
13

You can't put "return" inside of another function and expect it to return to the outside function. (Biggest problem)

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".

You could write the code like this instead:

  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;
Churl answered 27/8, 2018 at 12:48 Comment(0)
E
7

Use Promise for this if you really want to send response after 1000 otherwise there is no need for doing this.

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;
Endsley answered 27/8, 2018 at 11:14 Comment(0)
S
0

Why don't you just use 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;
Superstition answered 27/8, 2018 at 11:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.