没有
.always像jQuery一样的东西吗?如果没有,我该如何实现?
您可以这样实现
finally自己的方法:
Promise.prototype.finally = function(cb) { const res = () => this const fin = () => Promise.resolve(cb()).then(res) return this.then(fin, fin);};
或更广泛地讲,将解析信息传递给回调:
Promise.prototype.finally = function(cb) { const res = () => this return this.then(value => Promise.resolve(cb({state:"fulfilled", value})).then(res) , reason => Promise.resolve(cb({state:"rejected", reason})).then(res) );};
两者都确保原始解析得以维持(当回调中没有异常时),并确保等待诺言。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)