【发布时间】:2019-05-02 22:39:13
【问题描述】:
我一直在尝试遍历一组 ID 并将它们传递给调用 API 的函数 (this.handleTransfer)。我希望下一次迭代仅在收到上一次迭代的响应时才开始。 我决定用谷歌搜索如何使用承诺来做到这一点。 但是当我实现我上网的时候,第一次迭代后出现以下错误:
TypeError: e(...).then is not a function.
源是这个代码块的错误
return e().then(Array.prototype.concat.bind(t))
我的代码如下:
const promiseSerial = funcs =>
funcs.reduce((promise, func) =>
promise.then(result => func().then(Array.prototype.concat.bind(result))),
Promise.resolve([]))
const payments = this.payIDArray;
const funcs = payments.map(payment => () => this.handleTransfer(payment))
promiseSerial(funcs)
.then(console.log.bind(console))
.catch(console.error.bind(console))
我正在使用 VueJS 框架。
【问题讨论】:
-
this.handleTransfer(payment)不返回承诺? -
异步等待呢?构建您的序列会容易得多
-
而
.then(res => [...result, res])比函数绑定更具可读性 -
@JonasWilms 感谢您指出这一点。显然 this.handleTransfer(payment) 正在返回任何承诺......
标签: javascript arrays vue.js promise sequence