最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - superagentsupertest with asyncawait - Stack Overflow

programmeradmin6浏览0评论

Goal is to set the variable auth correctly for further use, hence i want to refactor the function loginUser:

function loginUser(user, request, auth) {
  return function(done) {
    request
      .post('/users/login')
      .send(credentials)
      .expect(200)
      .end(onResponse);

    function onResponse(err, res) {
      auth.token = res.body.token;
      return done();
    }
  };
}


 loginUser(user, request, auth)(function() {
  request.get(testUrl)
    .set('Authorization', `bearer ${auth.token}`)
    .expect(200, done);
});

to use async / await like this (without the callback):

auth = await loginUser(user, request);
request.get(testUrl)
    .set('Authorization', `bearer ${auth.token}`)
    .expect(200, done);

But i am struggling of returning / setting auth correctly (it would not matter if i pass auth as parameter or as return value).

What i tried was stuff like this:

async function loginUser(user, request) {
  let auth;
  await request
    .post('/users/login')
    .send(credentials)
    .expect(200)
    .end(onResponse);

  function onResponse(err, res) {
    auth.token = res.body.token;
  }
  return auth;
}

But auth was never set correctly.

Goal is to set the variable auth correctly for further use, hence i want to refactor the function loginUser:

function loginUser(user, request, auth) {
  return function(done) {
    request
      .post('/users/login')
      .send(credentials)
      .expect(200)
      .end(onResponse);

    function onResponse(err, res) {
      auth.token = res.body.token;
      return done();
    }
  };
}


 loginUser(user, request, auth)(function() {
  request.get(testUrl)
    .set('Authorization', `bearer ${auth.token}`)
    .expect(200, done);
});

to use async / await like this (without the callback):

auth = await loginUser(user, request);
request.get(testUrl)
    .set('Authorization', `bearer ${auth.token}`)
    .expect(200, done);

But i am struggling of returning / setting auth correctly (it would not matter if i pass auth as parameter or as return value).

What i tried was stuff like this:

async function loginUser(user, request) {
  let auth;
  await request
    .post('/users/login')
    .send(credentials)
    .expect(200)
    .end(onResponse);

  function onResponse(err, res) {
    auth.token = res.body.token;
  }
  return auth;
}

But auth was never set correctly.

Share Improve this question edited Jul 20, 2018 at 9:21 Gobliins asked Jul 20, 2018 at 9:03 GobliinsGobliins 4,02618 gold badges71 silver badges130 bronze badges 2
  • Possible duplicate of Promises es6 and superagent – k0pernikus Commented Jul 20, 2018 at 9:12
  • hmm not sure, since i want to get rid of the callback when calling the login function – Gobliins Commented Jul 20, 2018 at 9:18
Add a ment  | 

2 Answers 2

Reset to default 11

Don't use 'end' syntax, that's for callbacks:

const response = await request.post(...)
  .expect(200)
const {body: {token}} = response
return token

Basically it should look like sync code

The problem is that the onResponse method is being executed later than you the return of the function because of the event loop in Nodejs. So you will have to do resolve the promise exactly when you receive the data

The method loginUserInternal could be like this:

function loginUserInternal(user, request) {
  return new Promise((resolve,reject) => {
    let auth = {};
    request
      .post('/users/login')
      .send({
        username: user.username,
        password: user.password_decoded,
      })
      .expect(200)
      .end(onResponse);
    function onResponse(err, res) {
      if(err) return reject(err)
      auth.id = res.body.id;
      auth.token = res.body.token;
      auth.tokenExpires = res.body.tokenExpires;
      resolve(auth)
    }
  })
}

And call it like you were doing with async await.

发布评论

评论列表(0)

  1. 暂无评论