gpt4 book ai didi

javascript - 为什么 Reflux.js ListenAndPromise 帮助程序不起作用?

转载 作者:行者123 更新时间:2023-11-28 07:15:04 27 4
gpt4 key购买 nike

我正在使用 qwest 查询我的端点,如下所示,onGetResourceCompleted 处理程序按预期触发,但数据未定义。为什么?

var Actions = Reflux.createActions({
'getResource': { asyncResult: true }
});

Actions.getResource.listenAndPromise(function (id) {
return qwest.get('http://localhost:8000/my-data/'+id, null, { withCredentials: true });
});


var MyStore = Reflux.createStore({

listenables: Actions,

init: function () {
Actions.getResource('');
},

onGetResourceCompleted: function (data) {
console.log('OK', data); // Get's called but data is undefined. Why?
}

});

通过查看开发工具以及单独调用 qwest,我可以正确地看到数据加载:

qwest.get('http://localhost:8000/my-data/'+id, null, { withCredentials: true }).then(function(data) {
console.log('OK', data);
});

还执行以下工作:

ServiceActions.getResource.listen(function (id) {
ServiceActions.getResource.promise(
qwest.get('http://localhost:8000/my-data/'+id, null, { withCredentials: true })
);
});

最佳答案

我已经在您在github.com/spoike/refluxjs打开的原始问题中对这个“已确认的错误”的原因发表了一些评论。 。

所以,尽管您按照预期的方式使用回流功能,并且它们肯定会在不返回比赛结果的情况下创造竞争条件,但我认为您很幸运。事实证明,当您已经有可用的 promise 时,您在这种类型的请求的组合中使用的两个特定功能有点多余。我建议您完全删除 onGetRequestCompleted 处理程序,并使用处理已解决 promise 的标准 promise 方式来处理完成,这无论如何都会给您带来更大的灵 active 。

例如:

var MyStore = Reflux.createStore({

listenables: Actions,

init: function () {
Actions.getResource('')
.then() <-- this eliminates the need for onGetResourceCompleted
.catch() <-- or this instead/in addition
.finally() <-- or this instead/in additon
},

// no more onGetResourceCompleted

});

关于javascript - 为什么 Reflux.js ListenAndPromise 帮助程序不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30927589/

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