将Promise处理程序功能绑定到对象 [英] Binding a promise handler function to an object

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

问题描述

我有一些类似的代码:

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

如何在 bar 的上下文中将 failure 函数绑定到 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 :

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

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

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