gpt4 book ai didi

javascript - 如何在一系列依赖函数中构造 JavaScript 回调?

转载 作者:行者123 更新时间:2023-12-03 06:09:23 25 4
gpt4 key购买 nike

我只使用了简单的回调,即执行异步 AJAX 调用并在完成后回调的函数。每当事情变得更加复杂时,我就会使用 $.Deferred()。问题是每次处理 Promise 都会有很多代码,我想知道如何正确使用回调。

这是我的例子。 (问题出现在 secondary() 的返回中):

function primary()
{
//call secondary with self as callback
var data = secondary("someStr", primary);
if (data !== false) {
//logic that uses data
}
}

function secondary(str, callback)
{
//call the third function. Since we need parameters
//on the callback, we create anon function
var returnFromThird = tertiary(function() {
secondary(str, callback);
});

if (returnFromThird !== false) {
//when we have data from third do someting....
//but here lies the problem, how do i callback primary()?
return returnFromThird + " " + str;
} else {
//third is not yet ready
return false;
}
}

var myVar3 = null;
function tertiary(callback)
{
if (myVar3 === null) {
//do async ajax call that sets myVar3 value
var ajaxRequest = $.ajax({
url: "/someUrl",
type: "POST",
data: {myData : "blabla"},
async: true,
});
ajaxRequest.done(function(data) {
myVar3 = data;
//issue the call back, so secondary() can come get myVar3
callback();
});
//we did not have the required data so we return false
return false;
} else {
return myVar3;
}
}

//execute
primary();

这是我通常如何使用 JQuery Deferred 处理问题:

function primary()
{
var promise = secondary(str);
$.when(promise).then(
function (data) {
//logic that uses data
}
);
}

function secondary(str)
{
var dfd = $.Deferred();
var promise = tertiary();
$.when(promise).then(
function (data) {
dfd.resolve(data + " " + str);
}
);
return dfd.promise();
}

var myVar3 = null;
function tertiary()
{
var dfd = $.Deferred();
if (myVar3 === null) {
var ajaxRequest = $.ajax({
url: "/someUrl",
type: "POST",
data: {myData : "blabla"},
async: true,
});
ajaxRequest.done(function(data) {
myVar3 = data;
dfd.resolve(myVar3);
});

} else {
dfd.resolve(myVar3);
}
return dfd.promise();
}

primary();

最佳答案

如果您使用回调,则应始终调用回调,而不是有时返回值:

var myVar3 = null;
function tertiary(callback) {
if (myVar3 === null) {
//do async ajax call that sets myVar3 value
$.post("/someUrl", {myData : "blabla"}).done(function(data) {
myVar3 = data;
callback(data); // just pass the data to the callback
});
} else {
return callback(myVar3); // always call the callback with the data
}
}

现在您的其他函数将如下所示:

function primary() {
secondary("someStr", function(data) {
//logic that uses data
});
}

function secondary(str, callback) {
tertiary(function(returnFromThird) {
callback(returnFromThird + " " + str);
})
}

但是你是对的,你应该使用 promise ,它大大简化了这一点:

var myVarPromise = null;
function tertiary() {
if (myVarPromise === null)
myVarPromise = $.post("/someUrl", {myData : "blabla"});
return myVarPromise;
}
function primary() {
return secondary("someStr").then(function(data) {
//logic that uses data
});
}
function secondary(str) {
return tertiary().then(function(returnFromThird) {
return returnFromThird + " " + str;
});
}

关于javascript - 如何在一系列依赖函数中构造 JavaScript 回调?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39389851/

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