gpt4 book ai didi

javascript - Jquery .then 链行为

转载 作者:行者123 更新时间:2023-11-28 05:43:02 25 4
gpt4 key购买 nike

我有以下函数,它返回一个带有分配的 .then 函数的函数。

Func = function () {
return Func1([a, b])()
.then(_.bind(function () {
//something
}, this));
};

然后我将另一个 .then 函数分配给 Func,如下所示:

Func2 = function(){
//something
this.Func()
.then(_.bind(function () {
//something
}, this));
};

是否会有类似 Func1().then().then() 或其他东西的链?

最佳答案

这是一个promise 。只要 promise ,你就能做很多事then随你便! ;)

function callback() {
console.log("then");
}

var p = new Promise(function(resolve) {
resolve();
}).then(callback);

p.then(callback);
p.then(callback).then(callback);
p.then(callback).then(callback).then(callback);

<强> Working example.

或者就你的例子来说:

var Func1 = function(param1) {
return new Promise(function(resolve) {
resolve();
});
}

var Func = function() {
return Func1(["a", "b"]).then(function() {
console.log("then");
}.bind(this));
};

var Func2 = function() {
Func().then(function () {
console.log("then");
}.bind(this))
.then(function() {
console.log("then chain");
});
};

Func2();

<强> Working example.

关于javascript - Jquery .then 链行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38763566/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com