将承诺处理函数绑定到对象 [英] Binding a promise handler function to an object

查看:20
本文介绍了将承诺处理函数绑定到对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些代码,例如:

var bar = foo().then(function success(value) {
  // compute something from a value...
}, function failure(reason) {
  // handle an error...
});

如何将failure 函数绑定到bar 上下文中的this 对象.我知道我将不得不使用 myFunc.bind(this) 但我用什么代替 myFunc?

How do I bind the failure function to the this object in the context of bar. I know I will have to use myFunc.bind(this) but what do I substitute in place of myFunc?

推荐答案

你可以像这样使用 bind :

You can use bind like this:

var bar = foo().then(function success(value) {
  // compute something from a value...
}, function failure(reason) {
  // handle an error...
}.bind(this));

这篇关于将承诺处理函数绑定到对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆