Chain: a utility function for avoiding callback hell

Showcase your libraries, tools and other projects that help your fellow love users.
User avatar
airstruck
Party member
Posts: 650
Joined: Thu Jun 04, 2015 7:11 pm
Location: Not being time thief.

Chain: a utility function for avoiding callback hell

Post by airstruck »

Have some code that looks like this?

Code: Select all

print 'fading in'
Timer.add(1, function ()
    print 'showing splash screen'
    Timer.add(1, function ()
        print 'showing title screen'
        Timer.add(1, function ()
            print 'playing demo'
        end)
    end)
end)
Oh no, callback hell. Use Chain to avoid deeply nested callbacks.

Code: Select all

Chain(function (continue)
    print 'fading in'
    Timer.add(1, continue)
end)(function (continue)
    print 'showing splash screen'
    Timer.add(1, continue)
end)(function (continue)
    print 'showing title screen'
    Timer.add(1, continue)
end)(function (continue)
    print 'playing demo'
end)()
Documentation and stuff forthcoming. Gist contains another example showing how callback arguments are handled. Looking for feedback; if you can think of a way to make this neater or think there are features missing let me know.

https://gist.github.com/airstruck/b610e10800e94adc980f
User avatar
Ulydev
Party member
Posts: 445
Joined: Mon Nov 10, 2014 10:46 pm
Location: Paris
Contact:

Re: Chain: a utility function for avoiding callback hell

Post by Ulydev »

My text editor will thank you ! :rofl:

Image
User avatar
airstruck
Party member
Posts: 650
Joined: Thu Jun 04, 2015 7:11 pm
Location: Not being time thief.

Re: Chain: a utility function for avoiding callback hell

Post by airstruck »

Ulydev wrote:My text editor will thank you ! :rofl:
That's exactly the kind of thing I had in mind. If you end up using Chain for that, I would love to see another screenshot.
User avatar
unek
Citizen
Posts: 86
Joined: Fri Oct 12, 2012 8:43 pm
Location: Poland
Contact:

Re: Chain: a utility function for avoiding callback hell

Post by unek »

Ulydev wrote:My text editor will thank you ! :rofl:

Image
I didn't think this was possible outside node.js
DRAW DRAW - online painting app CLICK NUMBERS - cool game with numbers THEME LOVEFRAMES - a collection of my themes HOST FOR FREE - free hosting for online LOVE games STALK PEOPLE - a map of LOVE users
User avatar
airstruck
Party member
Posts: 650
Joined: Thu Jun 04, 2015 7:11 pm
Location: Not being time thief.

Re: Chain: a utility function for avoiding callback hell

Post by airstruck »

Have been playing with a coroutine-based alternative to Chain for flattening async code. No documentation yet, but here's a preview.

https://gist.github.com/airstruck/e4f11dc803848c158ca0

The wait function will return anything passed into the callback generated by continue. If continue is called more than once before a call to wait, only the first of those generated callbacks to be invoked will cause execution to continue.
User avatar
shakesoda
Citizen
Posts: 78
Joined: Thu Sep 25, 2014 11:57 am
Location: Seattle, WA
Contact:

Re: Chain: a utility function for avoiding callback hell

Post by shakesoda »

The coroutine-based version is way, way better to write code for. This is awesome.
excessive ❤ moé (LÖVE3D, CPML, ...). holo on IRC.
User avatar
farzher
Prole
Posts: 42
Joined: Fri Jul 31, 2015 5:38 pm

Re: Chain: a utility function for avoiding callback hell

Post by farzher »

Not a bad idea :) node.js is my main language

Although I think next is the standard name for the callback function
continue usually means something else in programming languages
(also next is shorter)
butts
User avatar
airstruck
Party member
Posts: 650
Joined: Thu Jun 04, 2015 7:11 pm
Location: Not being time thief.

Re: Chain: a utility function for avoiding callback hell

Post by airstruck »

farzher wrote:Not a bad idea :) node.js is my main language

Although I think next is the standard name for the callback function
continue usually means something else in programming languages
(also next is shorter)
I considered 'next' but it's a global in Lua. Of course you can name it whatever you like since it's just a parameter to a user defined function; neither solution here exposes any fixed identifiers. I sort of like "go" or "callback" too.
User avatar
farzher
Prole
Posts: 42
Joined: Fri Jul 31, 2015 5:38 pm

Re: Chain: a utility function for avoiding callback hell

Post by farzher »

time thief wrote:I considered 'next' but it's a global in Lua. Of course you can name it whatever you like since it's just a parameter to a user defined function; neither solution here exposes any fixed identifiers. I sort of like "go" or "callback" too.
I didn't realize `next` was a thing
While looking that up I also noticed lua has `goto`
They made some weird decisions on features. We don't get the basic continue, yet we get the dangerous goto
That's interesting though. Maybe I should refactor some loops to use `goto continue` instead of nesting if statements (slightly off topic, but we are talking about nesting)
butts
User avatar
ejmr
Party member
Posts: 302
Joined: Fri Jun 01, 2012 7:45 am
Location: South Carolina, U.S.A.
Contact:

Re: Chain: a utility function for avoiding callback hell

Post by ejmr »

farzher wrote:While looking that up I also noticed lua has `goto`. They made some weird decisions on features. We don't get the basic continue, yet we get the dangerous goto.
Personally I don't think that's weird or dangerous. Continue can be implemented in terms of goto. But the reverse isn't possible.
ejmr :: Programming and Game-Dev Blog, GitHub
南無妙法蓮華經
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 0 guests