Javascript 访问jQuery$。从Bluebird Promise对象获取URL
我在jQueryJavascript 访问jQuery$。从Bluebird Promise对象获取URL,javascript,jquery,promise,bluebird,Javascript,Jquery,Promise,Bluebird,我在jQuery$中使用蓝鸟承诺。get()如下: 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
$中使用蓝鸟承诺。get()
如下:
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块中访问$。使用选项获取URL?请参阅代码中的注释-当我捕获超时时,我需要知道用户在其请求中请求了什么
提供的示例被简化,我将承诺传递给另一个函数,并在那里访问它;我得到的是:
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的作用域:
我认为您不应该在另一个模块中处理超时,无论如何,您可以通过promise
p.userUrl=url.address传递url代码>。你怎么取消这个请求?@CrisimIlNumenoreano`你不能向然后传递一个承诺,而是一个回调。
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);
});