Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

CoffeeScript recursive function


May 09, 2021 CoffeeScript


Table of contents


Recursive function

Problem

You want to call the same function in a function.

Solution

Use a named function:

ping = ->
    console.log "Pinged"
    setTimeout ping, 1000

If it is an unnamed function, use @arguments.callee:

delay = 1000

setTimeout((->
    console.log "Pinged"
    setTimeout arguments.callee, delay
    ), delay)

Discuss

Although arguments.callee allows recursion of unnamed functions, which has some advantages in memory-intensive applications, named functions are relatively clear-purpose and easier to maintain code.