Monday 1 May 2017

node js async/await request in function

Hi I've some trouble with async/await in nodejs. I would use this into my factory in this way

//foo.js    

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

   function Foo(options) {
        this.options = options;
    }

    function create() {
        return new Foo(login());
    }

    async function login() {
        const options = {
            method: `POST`
            ,json: true
            ,uri: `http://xxxxxx/login`
            ,body: {username: 'abcd', password: '1234'}
        };
        try {
            const response = await rp(options);
            return (response);
        }
        catch (error) {
            return Promise.reject(error);
        }
    }
  module.export.create = create;

So I call create into my test and I'm expecting that return will be execute after login, but flow isn't this!!!

var Foo = require('foo');

describe('using my utils into project', function () {

        it('using real case', function (done) {

            var foo = Foo.create();
            console.log(foo.options);
            done();
        });

    });

Test return me OK but in console I see Promise { <pending> } not real response after login process. Where is my error? Is correct working in this way?



via Claudio Pomo

No comments:

Post a Comment