NodeJS Promises 返回 Pending
NodeJS Promises returning Pending
上下文:我正在尝试使用 jest 和 supertest 为我正在编写的 MongoDB 应用程序设置测试
目的:将一个值 return 从 postArticleByAPI 函数赋值给常量 id。
问题:returning Promise { <pending> }
我尝试过的:
- Promise.resolve(postArticleByAPI) 导致同样的问题。
- 链接 .then((res) => {console.log(res}) 结果为
undefined
.
我想我从根本上不理解承诺,也就是如何在承诺之外为 return 赋值。这可能吗?有人有什么建议吗?
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);
});
});
确实postArticleByAPI
return是Promise
而且在您记录时它还没有解决。你应该这样写:
describe('Test POST through API', () => {
test('It should response the POST method /api/articles', async () => {
const id = await postArticleByAPI(articleData);
console.log(id);
});
});
也不要忘记 return 来自 postArticleByAPI
的承诺:
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;
});
}
如果要使用async
和await
,则不应使用.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;
}
上下文:我正在尝试使用 jest 和 supertest 为我正在编写的 MongoDB 应用程序设置测试
目的:将一个值 return 从 postArticleByAPI 函数赋值给常量 id。
问题:returning Promise { <pending> }
我尝试过的:
- Promise.resolve(postArticleByAPI) 导致同样的问题。
- 链接 .then((res) => {console.log(res}) 结果为
undefined
.
我想我从根本上不理解承诺,也就是如何在承诺之外为 return 赋值。这可能吗?有人有什么建议吗?
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);
});
});
确实postArticleByAPI
return是Promise
而且在您记录时它还没有解决。你应该这样写:
describe('Test POST through API', () => {
test('It should response the POST method /api/articles', async () => {
const id = await postArticleByAPI(articleData);
console.log(id);
});
});
也不要忘记 return 来自 postArticleByAPI
的承诺:
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;
});
}
如果要使用async
和await
,则不应使用.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;
}