【发布时间】:2017-10-25 18:36:09
【问题描述】:
我正在尝试编写一个可以在更改解析值的承诺链中使用的函数。
下面,我希望函数 getAndChangeValue() 将解析的参数从“Hello”更改为“Bye”。请帮忙!我似乎无法理解它。 :-)
https://plnkr.co/edit/RL1XLeQdkZ8jd8IezMYr?p=preview
getAndChangeValue().then(function(arg) {
console.log(arg) // I want this to say "Bye"
});
function getAndChangeValue() {
var promise = getValue()
promise.then(function(arg) {
console.log('arg:', arg) // says "Hello"
// do something here to change it to "Bye"
})
return promise
}
function getValue() { // returns promise
return $.ajax({
url: "myFile.txt",
type: 'get'
});
}
【问题讨论】:
-
这是一个非常奇怪的要求。如果您要忽略返回的内容,为什么还要麻烦发出 AJAX 请求?
-
是的,这不是必需的。我只是在创建一个虚假的概念验证情况。我可以做一个超时,或者类似的事情
标签: javascript jquery promise jquery-deferred