我使用jQuery$.get()
这样的Bluebird Promise :
var p = Promise.resolve($.get(url.address, url.options, 'json')).then(function (result) {...
并通过以下方式处理它们:
p.timeout(100).catch(Promise.TimeoutError, function (error) {
console.log(p); // here I want to log in that Promise took too long to execute and access what user actually asked for
});
如何在上面的catch块中访问带有选项的$ .get URL?查看代码中的注释-当我超时时,我需要知道用户在其请求中要求的内容。
提供的示例已简化,我将Promise传递给另一个函数并在其中访问它。我要去的地方是:
Promise {
_bitField: 201326593,
_cancellationParent: undefined,
_fulfillmentHandler0: undefined,
_progressHandler0: undefined,
_promise0: undefined,
_receiver0: undefined,
_rejectionHandler0: undefined,
_settledValue: SubError,
__proto__: Promise
}
我不是在问$.get()
本质上如何获取url,而是要如何在Bluebird Promise的范围内获取它。
您不应该直接访问promise对象,而应由框架来处理它。如果要传递参数,则可以在promise中返回参数值(但是它将仅保留在下一个中),也可以使用promise的范围:https : //github.com/petkaantonov/bluebird/blob/master/ API.md#binddynamic-thisarg ---承诺
var result = Promise.bind({url: url})
.then(function(){
return Promise.resolve($.get(url.address, url.options, 'json'));
}
.then(function() {
// access your url in then
console.log('then: ', this.url);
return Promise.reject();
})
.catch(function(err) {
// access your url in catch
console.log('catch: ', this.url);
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句