如何使用done()或next()将参数传递给下游函数 [英] How pass parameters to downstream function with done() or next()

查看:87
本文介绍了如何使用done()或next()将参数传递给下游函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是next(),done()等的新手,并且在串行执行/可能是异步功能的链之间传播参数时感到困惑.

I am new to next(), done() etc. and am struggling with propagating parameters between serial executions/chaining of possibly otherwise asynchronous functions.

我想强制2个函数的串行执行,以便可以用以下任意一种调用它们:

I want to force serial execution of 2 functions so they can be called with something like either:

f1('#{arg1a}', '#{arg1b}').done(
  f2('#{arg2a}', '#{arg2b}')
);

或类似的东西:

f1('#{arg1a}', '#{arg1b}', '#{arg2a}', '#{arg2b}').done(f2);

使用JSF从查询参数中收集传入的arg值.

Where the arg values passed in are gleaned from query parameters using JSF.

请注意:

    在我的情况下,
  • arg2aarg2barg1aarg1b完全无关,并且f2的调用不以任何方式取决于f1中发生的事情,其他即使必须f1通常是异步的,它也必须严格执行.

  • arg2a and arg2b are in my case completely unrelated to arg1a and arg1b, and the invocation of f2 does NOT depend in any way on what happens in f1, other than it must execute strictly afterwards, even if f1 is otherwise usually asynchronous.

我尚未在此处定义即时匿名函数或诸如此类的done()内部,我希望能够使用某些已知参数调用库定义的函数.

I am not defining on-the-fly anonymous functions or such inside done() here (yet), I want to be able to call a library-defined function with some known params.

在此示例中,功能将类似于:

In this example, the functions would be something like:

function f1(arg1a, arg1b) {

   //do something with arg1a, arg1b

  return $.Deferred().resolve();
}

function f2(arg2a, arg2b) {
  // Do something with arg2a and arg2b AFTER f1 has fully run.
}

或类似的东西:

function f1(arg1a, arg1b, arg2a, arg2b) {

   //do something with arg1a, arg1b

  // Somehow ensure f1 is finished then execute f2(arg2a, arg2b)
}

function f2(arg2a, arg2b) {
  // Do something with arg2a and arg2b AFTER f1 has fully run.
}

仅使用回调链接对我正在解决的情况不起作用.另请参阅:如何在ap:tabview的ap:tab内的p:accordionPanel中链接并定位/打开ap:tab

Just using callback chaining did not work for the situation I am tackling. See also: How link to and target/open a p:tab within an p:accordionPanel within a p:tab within a p:tabview

一个可接受的答案必须允许我使用带有预定义参数的预定义函数f2

推荐答案

您需要将参数传递给.resolve(),然后使用.then()

You need to pass parameters to .resolve(), then use .then()

function f1(arg1a, arg1b) {

  return $.Deferred(function(dfd) {
    //do something with arg1a, arg1b
    // you can alternatively call `.resolve()` without passing parameters
    // when you are finished doing something with `arg1a`, `arg1b`,
    // which should call chained `.then()` where `f2` is called
    dfd.resolve(arg1a, arg1b)
  }).promise();
}

function f2(arg2a, arg2b) {
  // Do something with arg2a and arg2b AFTER f1 has fully run.
}

f1(arg1, arg2)
.then(function() {
  // call `f2` here
  f2('#{arg2a}', '#{arg2b}');
})
// handle errors
.catch(function(err) { // alternatively use `.fail()`
  console.log(err)
});

jsfiddle https://jsfiddle.net/wuy8pj8d/

jsfiddle https://jsfiddle.net/wuy8pj8d/

这篇关于如何使用done()或next()将参数传递给下游函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆