Javascript 在ajax成功的情况下,使用要激发的参数执行多个函数

Javascript 在ajax成功的情况下,使用要激发的参数执行多个函数,javascript,jquery,ajax,Javascript,Jquery,Ajax,我正在尝试使用jQuery编写几个函数,以帮助我测试一些用php编写的api端点。对于Javascript和jQuery,我是一个新手,我很难弄清楚我需要阅读什么才能让事情按我需要的方式运行 以下是我试图满足的要求: 调用应该是非阻塞的 仅在成功时按顺序触发函数 我需要将参数与函数一起传递,而不仅仅是函数名 每个函数都需要能够通过success:function(data)从api()函数中访问data变量返回 我已经阅读了jQuery文档,认为延迟和承诺可能是我应该追求的途径,但我无法让一

我正在尝试使用jQuery编写几个函数,以帮助我测试一些用php编写的api端点。对于Javascript和jQuery,我是一个新手,我很难弄清楚我需要阅读什么才能让事情按我需要的方式运行

以下是我试图满足的要求:

  • 调用应该是非阻塞的
  • 仅在
    成功时按顺序触发函数
  • 我需要将参数与函数一起传递,而不仅仅是函数名
  • 每个函数都需要能够通过
    success:function(data)
    api()
    函数中访问
    data
    变量返回
我已经阅读了jQuery文档,认为
延迟
承诺
可能是我应该追求的途径,但我无法让一个示例起作用

我的两个功能的简化版本:

(为清楚起见)

这就是我想要实现的目标(好的、简短的和可重用的):

fn1()
fn2()
按顺序激发,都可以访问
api()返回的数据

实现类似目标的最佳方式是什么?在正确的方向轻推将非常感谢


谢谢。

1-我将在匿名函数中使用这些方法作为参数

api('resources', 'get', 'document', {
    debug : true, // better to say true or false :)
    id : 7, // if your id is a number use number
    callback : function(data){
        fn1(data);
        fn2(data);
    }
});
2-如果你有很多事情要做,使用阵列可能会很酷

// in api method..
for(var i; i < cbks.lengths; i++){
    cbks[i](); // execute one by one the callbacks declared.
}

api('resources', 'get', 'document', {
    debug : true, // better to say true or false :)
    id : 7, // if your id is a number use number
    [fn1, fn2, fn3] // here are the callbacks
});
//在api方法中。。
对于(变量i;i
由于AJAX的异步特性,您无法直接从API函数链接函数。当链式函数运行时,数据将根本不可用。但是,您可以在success函数中执行此操作。您将需要构造一个包装器函数

function wrapper()
{
    return {
       fn1 : function(data){
           //execute code for fn1 here
           return wrapper();
       },
       fn2 : function(data){
           //execute code for fn2 here
           return wrapper();
       },
       alert : window.alert.bind(window);
       }
    }
}
通过在success函数中运行
wrapper.fn1(数据,参数).fn2(数据,参数).alert(“whoot!”)
,它将按照预期的方式工作

每次调用
包装器
函数或其中的函数时,它将返回允许函数链接的完整对象


更有效的设计是在包装器对象外部构造函数,只在包装器函数内部引用它们。

尝试在
$.ajax()
之前添加
return
语句,从
api
返回
数据,利用
.then()

function wrapper()
{
    return {
       fn1 : function(data){
           //execute code for fn1 here
           return wrapper();
       },
       fn2 : function(data){
           //execute code for fn2 here
           return wrapper();
       },
       alert : window.alert.bind(window);
       }
    }
}

请求的方式是不可能的。不能通过函数api链接异步请求。当触发
fn1
时,数据将不可用。fn1和fn2是否执行任何类型的异步操作?@Mouser你不能这么说!当然,他们需要能够从
api
访问数据。但这并不意味着fn1或fn2是异步的。@Ishettyl对不起,我误解了。问你的问题是有道理的@Mouser是的,我意识到数据还不可用,希望有一种方法将函数的执行推迟到可用。由于
SyntaxError:Unexpected token'(“function”属性名后面应该有“.一个“:”。
我是Javascript的新手,不知道输入错误在哪里。忘记在第一个示例的值之前添加键:)应该更好!这是我在阅读jQuery文档后开始的地方。在我的测试中,没有激发任何东西。当我尝试你的示例时,我遇到了相同的问题。@Matt“在我的测试中,没有激发任何东西。当我尝试你的示例时,我遇到了相同的问题。”请参阅更新的帖子。在
$.ajax()之前添加了
return
语句
将jQuery promise返回给
。然后()
$之前添加
返回
语句。ajax()
修复了它。您的解决方案工作得非常好。感谢您花时间帮助我完成此工作。我如何将此传递给api()函数?我不想在函数中硬编码任何内容。有几十个端点,有数百个参数组合,要用api()函数返回的数据进行测试,我需要尽可能地保持模块化和可重用。如果我没有理解,请道歉。
function wrapper()
{
    return {
       fn1 : function(data){
           //execute code for fn1 here
           return wrapper();
       },
       fn2 : function(data){
           //execute code for fn2 here
           return wrapper();
       },
       alert : window.alert.bind(window);
       }
    }
}
function api( api, method, endpoint, query ) {
    // return `$.ajax()` jQuery promise to `.then()`
    return $.ajax({
        type: method,
        url: '/api/' + api + '/' + endpoint,
        data: query,
        dataType: 'json',
        success: function(data) {

            // I currently have a single hardcoded function
            populate( data.result, '#resource', null );
            // return `data`
            return data

        },
        error: function( data ) {
            // Debug
            dump(data.result);
        }
    });

    // Do some other stuff here
}

// Example call
$('body').on('click', '#bigFatButton', function() {

    // I would like to specify a function (or chain of functions)
    // to be fired on success along with the api() function

    api('resources', 'get', 'document', {
        debug: '1',
        id: '7'
    }).then(function(data) {
         $.when(fn1(data, 'custom', 'params')
               , fn2(data, {other: 'params'})
         .then(function() {alert('wooty!')})
    })

});