承诺不尊重
我已经对此猛击了一段时间,但我无法弄清楚。
我有Main.js实例化用户对象并从中登录值。
main.js
jsUser = new user();
console.log(jsUser.passTimes);
的数据
i然后有user.js文件,该文件包含该类并获取Passtimes user.js
class user{
// construct the object
constructor(obj){
// just set stuff up, cuz why not?
this.pTimes = [];
}
loadPTimes(){
// return a promise
return new Promise((res,rej)=>{
$.ajax({
url: './data/importantfile',
type: 'post',
dataType: 'json',
data: {
method: 'getData',
termCode: termCode
},
success: (data)=>{
// trigger the resolve
res({
data: data,
class: this
});
}
});
});
}
get passTimes(){
// if pTimes is empty, or gone for some reason, do the following
if(
this.pTimes === undefined ||
this.pTimes.length == 0
){
// go get that promise and do stuff
this.loadPTimes().then((obj)=>{
// mom always told me I have value, and now I can prove it
this.pTimes = obj.data;
// send me up the chain
return this.pTimes;
});
}else{
// I always had value :), great send me up
return this.pTimes;
}
}
}
,对吗?但是,当页面实际上加载台时。log触发时,当Promise/Ajax仍在运行并返回未定义时。不是我所期望的。
我试图做的是,如果触发了get函数,我们没有确定的价值;使用Ajax获取数据,然后等待该数据返回,然后再返回某些内容。
我已经通过不同的结果重写了许多不同的方式,但没有我正在寻找的结果(Console.Log返回实际的Ajax结果)。
我希望这里有人使用了类似的模式或更好的模式。在返回之前等待数据。
编辑:
也许我的问题已经足够清楚。我需要AJAX数据,当构造类或调用 get 函数时。它必须在那里。
它不能异步,因为几乎需要该数据。因此,我将请求置于承诺中,期望承诺受到尊重,并且在具有数据时将其返回 get 函数。
这没有发生,而是 get 正在等待答复时,正在返回不确定的值。
I have been banging my head against this for a while now and I can't figure it out.
I have main.js who instantiates a user object and console logs a value from it.
main.js
jsUser = new user();
console.log(jsUser.passTimes);
I then have the user.js file, which houses the class and gets the data for passTimes
user.js
class user{
// construct the object
constructor(obj){
// just set stuff up, cuz why not?
this.pTimes = [];
}
loadPTimes(){
// return a promise
return new Promise((res,rej)=>{
$.ajax({
url: './data/importantfile',
type: 'post',
dataType: 'json',
data: {
method: 'getData',
termCode: termCode
},
success: (data)=>{
// trigger the resolve
res({
data: data,
class: this
});
}
});
});
}
get passTimes(){
// if pTimes is empty, or gone for some reason, do the following
if(
this.pTimes === undefined ||
this.pTimes.length == 0
){
// go get that promise and do stuff
this.loadPTimes().then((obj)=>{
// mom always told me I have value, and now I can prove it
this.pTimes = obj.data;
// send me up the chain
return this.pTimes;
});
}else{
// I always had value :), great send me up
return this.pTimes;
}
}
}
Easy enough right? However, when the page actually loads the console.log triggers while the promise/ajax is still running and returns undefined. Not what I was expecting.
What I was trying to do is, if the get function is triggered and we have no established value; go get the data using ajax and wait for that data to come back before return something.
I have rewritten this so many different ways with different results, but not the result I was looking for (console.log returns the actual ajax results).
I was hoping someone here has used a similar pattern, or a better one. That waits for data before returning.
Edited:
Maybe my questions was written clearly enough. I need ajax data, when the class is constructed or when the get function is called. It has to be there.
It can't be asynchronous because that data is needed almost immediately. So I throw the request in a promise, expecting the promise to be respected and the get function to return when it has data.
This is not happening, instead get is returning undefined as a value while the ajax request is still waiting for a reply.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这应该解决您的问题:
Passtimes将回报承诺,以获取必须使用
等待
或然后/CATCH CLASS
的值。This should fix your problem:
passTimes will return a promise so to get the value you must use
await
orthen/catch closure
有一些问题:
只有在 future 中可用时,就无法期望获得结果现在。因此,无论您做什么,都必须等待执行需要响应可用的逻辑。为此,您应该在
然后在
回调中执行该逻辑,或者使用等待
暂停函数的进一步执行。您的代码创建
新的Promise
当您已经有一个由$。$。ajax
返回的承诺时(除非您真正使用的是jQuery版本,否则十年历史)。您的代码检查
长度
分配给ptimes
的对象的属性,但是当您返回响应时,您将普通对象分配给该属性长度
属性。因此,检查该属性是错误的。这是做您想做的众多方法之一:
There are a few issues:
It is impossible to expect to get a result now, when it is only available in the future. So whatever you do, you must wait with executing logic that needs the response to be available. For this you should execute that logic in a
then
callback, or suspend a function's further execution withawait
.Your code creates a
new Promise
when you already have a promise that is returned by$.ajax
(unless you really use a version of jQuery that is more than a decade old).Your code checks the
length
property of the object assigned topTimes
, but when you get back the response, you assign a plain object to that property, which has nolength
property. So it is wrong to check that property.Here is one of the many ways to do what you want: