From d070a172d73c1cf00ddae5666192c6d0263c8a74 Mon Sep 17 00:00:00 2001 From: Thomas Perl Date: Sat, 19 Jul 2014 09:53:41 +0200 Subject: [PATCH] Documentation: Show how to avoid callback hell in JS. Fixes #24 --- docs/index.rst | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/docs/index.rst b/docs/index.rst index 94a608c..4e93095 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -479,6 +479,36 @@ scroll and move around in the UI) while the Python code can process requests. .. _Continuation-passing style: https://en.wikipedia.org/wiki/Continuation-passing_style +To avoid what's called `callback hell`_ in JavaScript, you can pull out the +anonymous functions you give as callbacks, give them names and pass them to +the API functions via name, e.g. the above example would turn into a shallow +structure (of course, in this example, splitting everything out does not make +too much sense, as the functions are very simple to begin with, but it's here +to demonstrate how splitting a callback hell pyramid basically works): + +.. _callback hell: http://callbackhell.com/ + +.. code-block:: javascript + + Python { + Component.onCompleted: { + function changedCwd(result) { + console.log('Working directory changed.'); + } + + function gotCwd(result) { + console.log('Working directory: ' + result); + call('os.chdir', ['/'], changedCwd); + } + + function withOs() { + call('os.getcwd', [], gotCwd); + } + + importModule('os', withOs); + } + } + Evaluating Python expressions in QML ````````````````````````````````````