承诺 returns 一个未定义的值
Promises returns an undefined value
我正在尝试用笑话和承诺进行测试。
当我尝试在控制台中写入时,结果是从 promise 中得到的,我有一个未定义的值
这是我的测试代码:
describe("Search concurrent with promises", () => {
describe("Test 10: Several searches in DDBB", () => {
function connection(){
let ddbb = new PGMappingDDBB();
return new Promise((resolve, reject) => {
let db = ddbb.connect();
//resolve("Hello World");
resolve(db);
});
}
test("Test 12: Several searches concurrently", () => {
connection().then(ddbb => {
console.log(ddbb);
});
});
});
});
ddbb.connect()
是一个异步函数。 connect() 的代码是:
async connect(){
this.client = await poolMapping.connect();
}
当我尝试写入变量的状态时 ddbb
我发现它是未定义的。
但是,如果我注释 resolve(db)
并删除 resolve("Hello World")
的注释,当我写入 ddbb
的值时,我得到 "Hello World"
.
我做错了什么?
编辑我:
ddbb.connect() returns 一个承诺。如果我写什么 returns console.log(ddbb.connect())。我有:
Promise { <pending> }
说明:
async connect(){
this.client = await poolMapping.connect();
// NOTE: There is no return value
}
describe("Search concurrent with promises", () => {
describe("Test 10: Several searches in DDBB", () => {
function connection(){
let ddbb = new PGMappingDDBB();
return new Promise((resolve, reject) => {
let db = ddbb.connect();
// ^^ this will be undefined, as connect has no return value
resolve(db);
// ^^^^^^^^^^^^ Resolving the promise with undefined
});
}
test("Test 12: Several searches concurrently", () => {
connection().then(ddbb => {
console.log(ddbb);
/// ^^^^ will be undefined
});
});
});
});
此外,jest 不会等待 promise 的结果,并且您不会在测试中断言任何内容,因此它总是会成功。
在处理 promise 时,如果您 return 来自测试的 promise,Jest 将等待它。但是您仍然需要对值进行断言。
我正在尝试用笑话和承诺进行测试。
当我尝试在控制台中写入时,结果是从 promise 中得到的,我有一个未定义的值
这是我的测试代码:
describe("Search concurrent with promises", () => {
describe("Test 10: Several searches in DDBB", () => {
function connection(){
let ddbb = new PGMappingDDBB();
return new Promise((resolve, reject) => {
let db = ddbb.connect();
//resolve("Hello World");
resolve(db);
});
}
test("Test 12: Several searches concurrently", () => {
connection().then(ddbb => {
console.log(ddbb);
});
});
});
});
ddbb.connect()
是一个异步函数。 connect() 的代码是:
async connect(){
this.client = await poolMapping.connect();
}
当我尝试写入变量的状态时 ddbb
我发现它是未定义的。
但是,如果我注释 resolve(db)
并删除 resolve("Hello World")
的注释,当我写入 ddbb
的值时,我得到 "Hello World"
.
我做错了什么?
编辑我:
ddbb.connect() returns 一个承诺。如果我写什么 returns console.log(ddbb.connect())。我有:
Promise { <pending> }
说明:
async connect(){
this.client = await poolMapping.connect();
// NOTE: There is no return value
}
describe("Search concurrent with promises", () => {
describe("Test 10: Several searches in DDBB", () => {
function connection(){
let ddbb = new PGMappingDDBB();
return new Promise((resolve, reject) => {
let db = ddbb.connect();
// ^^ this will be undefined, as connect has no return value
resolve(db);
// ^^^^^^^^^^^^ Resolving the promise with undefined
});
}
test("Test 12: Several searches concurrently", () => {
connection().then(ddbb => {
console.log(ddbb);
/// ^^^^ will be undefined
});
});
});
});
此外,jest 不会等待 promise 的结果,并且您不会在测试中断言任何内容,因此它总是会成功。
在处理 promise 时,如果您 return 来自测试的 promise,Jest 将等待它。但是您仍然需要对值进行断言。