【发布时间】:2018-05-05 18:27:16
【问题描述】:
我有一个包含值列表的数组,其中一些是需要解决的承诺。
allGlobalFilters": [
{
"id": 503,
"pubId": 18,
"type": 15,
"value": "{ \"adsize\": [\"638x335\" , \"400x300\" , \"300x1050\", \"320x100\", \"640x480\" , \"700x392\", \"360x480\", \"896x502\", \"960x538\", \"1024x573\", \"1088x609\"]}",
"status": 4,
"createTs": null,
"updateTs": null,
"createUser": "MMM",
"updateUser": null,
"percentage": 100,
"rtbSspPublishers": { // the promise part
"id": 18,
"name": "Mobile Nations\r\n",
"domain": "http://www.mobilenations.com",
"extPublisherId": 17
}
},
{
"id": 505,
"pubId": 19,
"type": 15,
"value": "{ \"adPlatformType\": [\"APP\"] }",
"status": 4,
"createTs": null,
"updateTs": null,
"createUser": null,
"updateUser": null,
"percentage": 0,
"rtbSspPublishers": { // the promise part
"id": 19,
"name": "Tom's Guide",
"domain": "www.tomsguide.com",
"extPublisherId": 17
}
},
{
"id": 514,
"pubId": 19,
"type": 15,
"value": "{ \"adPlatformType\": [\"WEB\"] }",
"status": 4,
"createTs": null,
"updateTs": null,
"createUser": null,
"updateUser": null,
"percentage": 100,
"rtbSspPublishers": { // the promise part
"id": 19,
"name": "Tom's Guide",
"domain": "www.tomsguide.com",
"extPublisherId": 17
}
},
{
"id": 516,
"pubId": 19,
"type": 15,
"value": "{\"adPlatformType\": [\"MOBILE_WEB\"]}",
"status": 4,
"createTs": null,
"updateTs": null,
"createUser": null,
"updateUser": null,
"percentage": 100,
"rtbSspPublishers": { // the promise part
"id": 19,
"name": "Tom's Guide",
"domain": "www.tomsguide.com",
"extPublisherId": 17
}
}
]
现在我需要在 promise 的条件下过滤这个数组,我该怎么做。
这是我尝试过的。
data.filter(d => d.rtbSspPublishers.get().then(res => res.name.includes('Mobile')));
但这似乎不起作用。如何让异步值与数组过滤器一起使用?
【问题讨论】:
-
您无法对异步数据进行同步过滤 - 您需要先等待 promise 解决,然后运行过滤器
-
@JaromandaX 我如何在这里等待同样的事情,我知道承诺是异步的,过滤器是同步的。谢谢你能不能给点提示
-
bluebird 有一个叫做
Promise.props的东西,可能是你想要的 -
您的代码是
rtbSspPublishers.get().then的事实...表明rtbSspPublishers不是 Promise,而是带有返回 Promise 的.get方法的某个对象,这可能使其成为稍微复杂一点
标签: javascript arrays