javascript - 一个接一个地解决一系列promise
问题描述
我为此找到了很多解决方案,通常是这样的
const serial = funcs =>
funcs.reduce((promise, func) =>
promise.then(result =>
func().then(Array.prototype.concat.bind(result))),
Promise.resolve([])
)
我正在尝试映射一系列承诺并一个接一个地运行它们,
serial(Object.keys(tables).map(key =>
websocketExecute(store,dropTableSQL(tables[key]),null)))
.then(data => {console.log(data);success(data)})
他们都运行但是我得到一个错误TypeError: func is not a function
然后最终没有解决..
知道如何在承诺列表上运行最终的 .then() 吗?
解决方案
你的函数serial
期望它的参数是一个返回 Promises 的函数数组
然而,
Object.keys(tables).map(key => websocketExecute(store,dropTableSQL(tables[key]),null))
返回调用结果的数组
websocketExecute(store,dropTableSQL(tables[key]),null)
这不太可能是一个返回承诺的函数,更像是一些结果
你要做的是:
serial(Object.keys(tables).map(key => () => websocketExecute(store,dropTableSQL(tables[key]),null)))
.then(data => {console.log(data);success(data)})
假设websocketExecute
返回一个 Promise
所以现在,返回的.map
数组是一个数组
() => websocketExecute(store,dropTableSQL(tables[key]),null)
哪个会被依次调用.reduce
推荐阅读
- gdb - 如何在不向应用程序传递信号的情况下进行步进?
- docker - 如何将项目代码从 docker 提取到本地系统
- .net - 将权限属性添加到 Swashbucke-Swagger-Documentation
- azure-container-service - 将现有 Web 应用程序移植到 Azure Service Fabric Mesh
- solr - Solr:尽管在 fieldType 上为用于 Faceting 的字段启用 docValues="true",但使用大量内存的字段缓存
- elasticsearch - elasticsearch-dsl 库中不存在 matrix_stats
- c++ - C ++双重充当浮点数?
- stripe-payments - 如何获得条带连接帐户的总支出?
- c++ - C++什么时候进行深拷贝和浅拷贝?
- java - SPNEGO - 从身份验证检查中排除特定 URL/URL 模式