How to run a series of functions, if one of them executes, return

javascript synchronous function execution
call a function after previous function is complete javascript
javascript wait for function to finish
javascript promise
async/await
callback function javascript
how to stop asynchronous call in javascript
javascript synchronous wait

I am initialising container view controllers in my prepareForSegue function like this:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    initialiseTopViewController(for: segue)
    initialiseMessagesViewController(for: segue)
    initialiseToolbarViewController(for: segue)
    initialiseTicketViewController(for: segue)
}

Only one of these functions will actually run per segue due to guard conditions on the segue identifier. How can I make it so that if one of these functions executes, it doesn't continue trying to execute the others? Is there a better way than just using if...else if blocks

Similar to @rob mayoff's suggestion, have each function return a Bool and loop through an array that holds all of them, making this completely data-driven.

func prepare(for segue: UIStoryboardSegue, sender: Any?) {
   let funs = [ initialiseTopViewController(for:), 
                initialiseMessagesViewController(for:), 
                initialiseToolbarViewController(for:), 
                initialiseTicketViewController(for:) ]

   for fun in funs {
       if fun(segue) {
           break
       }
   }
}

Executing arrays of async/await JavaScript functions in series vs , When dealing with an array of async/await functions (which return Promises), it can be tricky to figure out how to execute them all in series  Functions . Functions are "self contained" modules of code that accomplish a specific task. Functions usually "take in" data, process it, and "return" a result. Once a function is written, it can be used over and over and over again. Functions can be "called" from the inside of other functions.

One solution is to make each initialise function return true if it handles the segue, else false. Then:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    _ = (false
        || initialiseTopViewController(for: segue)
        || initialiseMessagesViewController(for: segue)
        || initialiseToolbarViewController(for: segue)
        || initialiseTicketViewController(for: segue)
    )
}

private func initialiseTopViewController(for segue: UIStoryboardSegue) -> Bool { ... }
private func initialiseMessagesViewController(for segue: UIStoryboardSegue) -> Bool { ... }
private func initialiseToolbarViewController(for segue: UIStoryboardSegue) -> Bool { ... }
private func initialiseTicketViewController(for segue: UIStoryboardSegue) -> Bool { ... }

Another solution is to hoist the appropriate tests up into prepare(for:sender:), for example using a switch:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    switch (segue.identifier ?? "", segue.destination) {
        case (_, let dest as TopViewController): initialiseTopViewController(dest)
        case (_, let dest as MessagesViewController): initialiseMessagesViewController(dest)
        case ("toolbar", let dest as MultipurposeViewController): initialiseToolbarViewController(dest)
        case ("ticket", let dest as MultipurposeViewController): initialiseTicketViewController(dest)
        default: super.prepare(for: segue, sender: sender)
    }
}

private func initialiseTopViewController(_ controller: TopViewController) { ... }
private func initialiseMessagesViewController(_ controller: MessagesViewController) { ... }
private func initialiseToolbarViewController(_ controller: MultipurposeViewController) { ... }
private func initialiseTicketViewController(_ controller: MultipurposeViewController) { ... }

Functions :: Eloquent JavaScript, A return keyword without an expression after it will cause the function to return Every time the function is called, new instances of these bindings are created. The exception is when multiple bindings have the same name—in that case, code can A run through this program goes roughly like this: the call to greet causes  In VBA programming, a function returns a value. You can execute Function procedures and call the function in Excel 2016. Functions, unlike Sub procedures, can be executed in only two ways: By calling the function from another Sub procedure or Function procedure By using the function in a worksheet formula Try this simple function.

I'd suggest a switch statement that checks/casts destination for various types, and passing that typed value to the appropriate method:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    switch segue.destination {
    case let destination as FooViewController: prepare(destination)
    case let destination as BarViewController: prepare(destination)
    case let destination as BazViewController: prepare(destination)
    default: super.prepare(for: segue, sender: sender)
    }
}

func prepare(_ destination: FooViewController) {
    destination.foo = foo
}

func prepare(_ destination: BarViewController) {
    destination.bar = bar
}

func prepare(_ destination: BazViewController) {
    destination.baz = baz
}

This switch only calls the appropriate method and saves those separate methods from needing to do any guard statements at all.

Embedded systems: architecture, programming and design, Before executing these instructions , also saved are the current program ' s other program contexts if not done automatically by the processor , and if these may be Numberings on the arrows show the sequences during the program run ( flow ) . The last instruction ( action ) of any routine or function is always for a return . Problem is you call First(), so after yield returns one item it's done with yielding. If you would call Take(2).First() then the code after first yield would also execute. – Dialecticus Sep 6 '13 at 9:49

I want to add to @rob mayoff's solution with making the functions return bool

// make each function return boolean values so you know if it executed properly or not based on the return value
func initialiseTopViewController(for segue: UIStoryboardSegue) -> Bool { ... }
func initialiseMessagesViewController(for segue: UIStoryboardSegue) -> Bool { ... }
func initialiseToolbarViewController(for segue: UIStoryboardSegue) -> Bool { ... }
func initialiseTicketViewController(for segue: UIStoryboardSegue) -> Bool { ... }

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    // if any function returns true, then return and don't run the remaining functions
    if initialiseTopViewController(for: segue) { return }
    if initialiseMessagesViewController(for: segue) { return }
    if initialiseToolbarViewController(for: segue) {return }
    if initialiseTicketViewController(for: segue) { return }
}

I'm sorry if this is similar to other suggestions. I just wanted to show you how to do achieve easily with in-line if statements.

Professional WordPress Plugin Development, If omitted, the function will check whether any filters are registered for the The function will return different values based on whether $function_to_check is There are cases where you might want the function to execute differently You could write two different filter functions, or you could combine them into one and use  USE [AdventureWorks2016CTP3] GO -- Declare a variable to return the results of the function. DECLARE @ret nvarchar(15); -- Execute the function while passing a value to the @status parameter EXEC @ret = dbo.ufnGetSalesOrderStatusText @Status = 5; -- View the returned value.

PC Mag, One of its most promising but least utilized new additions was function 4Ch, Function 4Ch allows a subprogram to terminate and return a l-byte code to the parent Normally when we load and execute a file, the parent program is none other DOS makes no provisions for multiple statements (as does BASIC), nor does it  function a() { // first function code here $(document).trigger('function_a_complete'); } function b() { // second function code here } $(document).bind('function_a_complete', b); Using this method, function 'b' can only execute AFTER function 'a', as the trigger only exists when function a is finished executing.

How to run async JavaScript functions in sequence or parallel, Where it can get tricky is when we have a whole bunch of tasks in an array. You might run through the array expecting calls to run one-by-one. But instead You run through your array in a loop, wanting them to run in parallel. function asyncTimeout(delay) { return (new Promise(resolve => {setTimeout(()  Functions that are executed before and after main() in C With GCC family of C compilers, we can mark some functions to execute before and after main(). So some startup code can be executed before main() starts, and some cleanup code can be executed after main() ends.

6.2. Functions that Return Values, For example, if you want to find the absolute value of a number, you have to So an important difference between these functions and one like drawSquare is that we wrote drawSquare because we wanted it to execute a sequence of steps  16 thoughts on “ Run a macro from a macro (from another workbook) ” fretta says: What if I want to share a macro with my team to execute a certain task where everybody is using their own ID and might be in a different network all across the country, even the world.

Comments
  • A switch (which is a if else/if) might be more readable though.
  • I think you'd better use a switch, because that's the very purpose of it: "A switch statement considers a value and compares it against several possible matching patterns. It then executes an appropriate block of code, based on the first pattern that matches successfully."