【发布时间】:2014-11-24 00:29:54
【问题描述】:
我在 SO 中找到了以下代码,我尝试对其进行调试以更好地理解 promise 概念,目前在下面的代码中有一些我不理解的东西,这就是为什么最后一个然后得到id==4 的值(在 specificTweet 中)而不是所有推文列表?
$.get('profile.json').then(function (profile) {
return $.get('tweets.json').then(function (response) {
return response.filter(function (tweet) {
//this is return that not related to the promise just to the filter
return tweet.id === 4;
});
});
}).then(function (specificTweet) {
...
这是 JSON 文件,
[
{
"id": 1,
"tweet": "OMG, worst day ever, my BF @BobbyBoo dumped me",
"usersMentioned": [
{
"id": 10,
"username": "BobbyBoo"
}
]
},
{
"id": 2,
"tweet": "OMG, best day ever, my BF came back to me"
},
{
"id": 3,
"tweet": "OMG, worst day ever, just don't ask"
},
{
"id": 4,
"tweet": "@BobbyBoo OMG...just OMG!",
"usersMentioned": [
{
"id": 10,
"username": "BobbyBoo"
}
]
}
]
【问题讨论】:
标签: javascript jquery json promise angular-promise