From e37aa56defb3fe4b268054afb6b629c1c3e24495 Mon Sep 17 00:00:00 2001 From: Aaron Fischer Date: Tue, 22 Aug 2017 23:44:46 +0200 Subject: [PATCH] Fix the promise hell --- src/act1.js | 9 ++++----- src/bubble.js | 30 +++++++++++------------------- 2 files changed, 15 insertions(+), 24 deletions(-) diff --git a/src/act1.js b/src/act1.js index f0f4e22..9d3ecab 100644 --- a/src/act1.js +++ b/src/act1.js @@ -3,11 +3,10 @@ muri.get('bubble') .story([ - [['Beep ...', 'Click, Click, Click ...'], [20, 15]], - [['Urgh ... ....', 'What is wrong with me? ...'], [5, 40]] - ]).then(function() { - console.log("asdf"); - }) + [['Beep', 'Bip, Bip'], [20, 15]], + [['Urgh ... ...', 'Where I am?', 'What happened?'], [35, 40]], + [['I can\'t see a thing ...', '... need to turn on the light ...'], [35, 40]] + ]); act1.update = function() { }; diff --git a/src/bubble.js b/src/bubble.js index 5636f57..d91c8b4 100644 --- a/src/bubble.js +++ b/src/bubble.js @@ -4,7 +4,7 @@ var isactive = false; var show = function(text, position) { return new Promise(function(resolve) { - isactive = true; + isActive = true; var dom = document.getElementById('bubble'); dom.style.display = ''; @@ -16,10 +16,10 @@ var show = function() { if (parts.length === 0) { setTimeout(function() { - isactive = false; + isActive = false; dom.style.display = 'none'; return resolve(); - }, 4000); + }, 2000); return; } dom.innerHTML += parts.shift() + ' '; @@ -30,26 +30,18 @@ }; bubble.talk = function(texts, position) { - return new Promise(function(resolve) { - if (texts.length === 0) { - return resolve(); - } - var text = texts.shift(); - show(text, position || [5, 40]).then(function() { - bubble.talk(texts, position); - }); + if (texts.length === 0) return; + var text = texts.shift(); + return show(text, position || [5, 40]).then(function() { + return bubble.talk(texts, position); }); }; bubble.story = function(talkList) { - return new Promise(function(resolve, reject) { - if (talkList.length === 0) { - return resolve(); - } - var params = talkList.shift(); - bubble.talk(params[0], params[1]).then(function() { - bubble.story(talkList); - }); + if (talkList.length === 0) return; + var params = talkList.shift(); + return bubble.talk(params[0], params[1]).then(function() { + return bubble.story(talkList); }); };