gpt4 book ai didi

javascript - 返回结果作为 Typescript 中的 promise

转载 作者:行者123 更新时间:2023-11-28 17:57:05 24 4
gpt4 key购买 nike

如何让下面的函数返回一个 promise ,以便我可以在调用该函数的页面中正确处理它?<​​/p>

getUploads() {
const rootDef = this.db.database.ref();
const uploadsRef = rootDef.child('userUploads').orderByChild('time');
const userRef = rootDef.child("userProfile");
var uploads = [];

uploadsRef.once("value").then((uploadSnaps) => {

uploadSnaps.forEach((uploadSnap) => {

var upload = uploadSnap.val();

userRef.child(uploadSnap.val().user).once("value").then((userSnap) => {
upload.displayName = userSnap.val().displayName;
upload.avatar = userSnap.val().avatar;
uploads.push(upload);
});

});

});

return uploads;
}

我尝试了以下方法,但显示错误。应该如何修改?

return new Promise((resolve, reject) => {
resolve(uploads);
});

我将调用此方法,如下所示。

this.db.getUploads().then((uploads) => {
this.allUploads = uploads;
console.log(this.allUploads);
});

最佳答案

我认为您可以将方法的内容括起来

getUploads() {
return new Promise((resolve, reject) => {
// content of method
resolve(uploads); // instead of "return uploads"
});
}

关于javascript - 返回结果作为 Typescript 中的 promise ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44244285/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com