已解决的承诺 returns 未定义,但它具有价值
resolved promise returns undefined though it has value
我遇到了一个令人沮丧的问题,我找不到它发生的原因
我有一个函数如下:
public async PageResult(searchRecord:Search,authorization:string,pageNumber:number,databaseName:string){
some codes .....
return new Promise((resolve, reject) => {
this._resultsDatastore.bulkInsert(
databaseName,
objects
).then(succ => {
// I can see succ is printed successfully in console
console.log(succ);
// when I resolve it here I assume I am returning the result
resolve(succ)
}, err => {
console.log(err);
reject(err)
})
})
}
所以你可以看到我解决了(成功),这样我就把成功返回给调用者函数
现在我有:
public async loadResults(
searchId: string,
authorization: string
) {
some code....
const results = udsQuery.pages.map(async (pageNumber: number) => await this.PageResult(searchRecord,authorization,pageNumber,databaseName))
let all: any=[];
await Promise.all(results).catch(e => {
console.log("^^^^^^^^^^^^^^^^^^^^^EEEERRRRRROR^^^^^^^^^^^^^^^^^^^^^^");
}).then(res=>{
console.log("suuuuuuuuuuuuuuucesssssssssssssss");
// issue is here: below prints undefined though succ has value
console.log(res);
all.push(res);
return res;
});
}
所以现在我在地图中调用 PageResult 并使用 promis.all 重新爱它而不是我在 PageResult 中返回的 succ 我得到未定义请参阅上面代码中的这一部分:
console.log("suuuuuuuuuuuuuuucesssssssssssssss");
// issue is here: below prints undefined though succ has value
console.log(res);
我错过了什么吗?
不知怎么的,我不喜欢你写代码的方式,我试着先修改那个代码。我还看到了另一个错误,你正在执行你的 PageResult()
函数,而不是它应该由 Promise.all
块执行。
我没有测试代码,但它应该是这样的:
public async PageResult(searchRecord:Search,authorization:string,pageNumber:number,databaseName:string){
try
{
const succ = await this._resultsDatastore.bulkInsert(
databaseName,
objects);
return succ;
}
catch (e)
{
throw e;
}
}
public loadResults(searchId: string, authorization: string )
{
const all: any=[];
const pormises: any=[];
const results = udsQuery.pages.map((pageNumber: number) =>
pormises.push(this.PageResult(searchRecord, authorization, pageNumber, databaseName)));
return new Promise((resolve, reject) =>
{
Promise.all(results)
.then(res=>
{
console.log("suuuuuuuuuuuuuuucesssssssssssssss");
// issue is here: below prints undefined though succ has value
console.log(res);
all.push(res);
return resolve(all);
})
.catch(e =>
{
console.log("^^^^^^^^^^^^^^^^^^^^^EEEERRRRRROR^^^^^^^^^^^^^^^^^^^^^^");
});
});
}
我遇到了一个令人沮丧的问题,我找不到它发生的原因
我有一个函数如下:
public async PageResult(searchRecord:Search,authorization:string,pageNumber:number,databaseName:string){
some codes .....
return new Promise((resolve, reject) => {
this._resultsDatastore.bulkInsert(
databaseName,
objects
).then(succ => {
// I can see succ is printed successfully in console
console.log(succ);
// when I resolve it here I assume I am returning the result
resolve(succ)
}, err => {
console.log(err);
reject(err)
})
})
}
所以你可以看到我解决了(成功),这样我就把成功返回给调用者函数
现在我有:
public async loadResults(
searchId: string,
authorization: string
) {
some code....
const results = udsQuery.pages.map(async (pageNumber: number) => await this.PageResult(searchRecord,authorization,pageNumber,databaseName))
let all: any=[];
await Promise.all(results).catch(e => {
console.log("^^^^^^^^^^^^^^^^^^^^^EEEERRRRRROR^^^^^^^^^^^^^^^^^^^^^^");
}).then(res=>{
console.log("suuuuuuuuuuuuuuucesssssssssssssss");
// issue is here: below prints undefined though succ has value
console.log(res);
all.push(res);
return res;
});
}
所以现在我在地图中调用 PageResult 并使用 promis.all 重新爱它而不是我在 PageResult 中返回的 succ 我得到未定义请参阅上面代码中的这一部分:
console.log("suuuuuuuuuuuuuuucesssssssssssssss");
// issue is here: below prints undefined though succ has value
console.log(res);
我错过了什么吗?
不知怎么的,我不喜欢你写代码的方式,我试着先修改那个代码。我还看到了另一个错误,你正在执行你的 PageResult()
函数,而不是它应该由 Promise.all
块执行。
我没有测试代码,但它应该是这样的:
public async PageResult(searchRecord:Search,authorization:string,pageNumber:number,databaseName:string){
try
{
const succ = await this._resultsDatastore.bulkInsert(
databaseName,
objects);
return succ;
}
catch (e)
{
throw e;
}
}
public loadResults(searchId: string, authorization: string )
{
const all: any=[];
const pormises: any=[];
const results = udsQuery.pages.map((pageNumber: number) =>
pormises.push(this.PageResult(searchRecord, authorization, pageNumber, databaseName)));
return new Promise((resolve, reject) =>
{
Promise.all(results)
.then(res=>
{
console.log("suuuuuuuuuuuuuuucesssssssssssssss");
// issue is here: below prints undefined though succ has value
console.log(res);
all.push(res);
return resolve(all);
})
.catch(e =>
{
console.log("^^^^^^^^^^^^^^^^^^^^^EEEERRRRRROR^^^^^^^^^^^^^^^^^^^^^^");
});
});
}