【问题标题】:NodeJS Promises returning Pending [duplicate]NodeJS承诺返回待处理[重复]
【发布时间】:2021-05-16 01:59:35
【问题描述】:

上下文:我正在尝试使用 jest 和 supertest 为我正在编写的 MongoDB 应用程序设置测试

目标:将从 postArticleByAPI 函数返回的值分配给常量 id。

问题:它正在返回Promise { <pending> }

我尝试过的:

  1. Promise.resolve(postArticleByAPI) 会导致同样的问题。
  2. 链接 .then((res) => {console.log(res}) 会导致 undefined

我认为我从根本上不理解承诺,或者即如何分配它们在承诺之外返回的值。这可能吗?有人有什么建议吗?

const articleData = {title: 'Hello123', doi: '123', journal: 'Facebook'};

/**
 * Posts an article through the API
 * @param {Object} article - the article objection containing dummy data
 * @return {string} request - the article id stored in the database
**/
async function postArticleByAPI(article) {
  await request(app)
      .post('/api/articles')
      .send(article)
      .expect(200)
      .then((response) => {
        expect(response.body.title).toBe(article.title);
        expect(response.body.doi).toBe(article.doi);
        expect(response.body.journal).toBe(article.journal);
        expect(response.body.id).toBeTruthy();
        return response.body.id;
      });
}


describe('Test POST through API', () => {
  test('It should response the POST method /api/articles', () => {
    const id = postArticleByAPI(articleData);
    console.log(id);
  });
});

【问题讨论】:

  • 你没有在函数中返回任何东西
  • 所有async 函数都返回一个承诺。他们就是这样工作的。您必须在返回的 Promise 上使用 .then()await 才能知道它何时完成工作和/或从中获取已解决的值(如果您决定从 async 函数返回一个值。跨度>
  • @Phix 是正确的,你忘了return。但是return await... 是一种反模式,您可以简单地编写return ...,因为所有async 函数都会自动返回promise。对于这个函数,由于没有await,你也可以删除async关键字,function postArticleByAPI(article) { return request(...) }的工作原理完全相同。

标签: javascript node.js promise es6-promise


【解决方案1】:

确实postArticleByAPI 返回一个Promise,并且在您记录它时尚未解决。你应该这样写:

describe('Test POST through API', () => {
  test('It should response the POST method /api/articles', async () => {
    const id = await postArticleByAPI(articleData);
    console.log(id);
  });
});

别忘了从postArticleByAPI返回Promise:

function postArticleByAPI(article) {
  return request(app)
      .post('/api/articles')
      .send(article)
      .expect(200)
      .then((response) => {
        expect(response.body.title).toBe(article.title);
        expect(response.body.doi).toBe(article.doi);
        expect(response.body.journal).toBe(article.journal);
        expect(response.body.id).toBeTruthy();
        return response.body.id;
      });
}

如果你想使用asyncawait,你不应该使用.then -

async function postArticleByAPI(article) {
  const response =
    await request(app)
      .post('/api/articles')
      .send(article)
      .expect(200)

  expect(response.body.title).toBe(article.title);
  expect(response.body.doi).toBe(article.doi);
  expect(response.body.journal).toBe(article.journal);
  expect(response.body.id).toBeTruthy();
  return response.body.id;
}

【讨论】:

  • 其实函数也不需要async了。
  • 既然没有await,你也应该去掉async关键字
  • 是的,谢谢指出
  • 第一个解决方案有效,但是,您必须将 async 放在测试中,而不是 describe,即使 vscode 似乎认为 await 是不必要的,它也是。
  • 是的,我正在解决这个问题
猜你喜欢
  • 2020-12-18
  • 2021-07-22
  • 1970-01-01
  • 2020-12-20
  • 1970-01-01
  • 1970-01-01
  • 2017-04-27
  • 1970-01-01
相关资源
最近更新 更多