Fix the promise hell

This commit is contained in:
Aaron Fischer 2017-08-22 23:44:46 +02:00
parent cc2ac3dbef
commit e37aa56def
2 changed files with 15 additions and 24 deletions

View file

@ -3,11 +3,10 @@
muri.get('bubble') muri.get('bubble')
.story([ .story([
[['Beep ...', 'Click, Click, Click ...'], [20, 15]], [['Beep', 'Bip, Bip'], [20, 15]],
[['Urgh ... ....', 'What is wrong with me? ...'], [5, 40]] [['Urgh ... ...', 'Where I am?', 'What happened?'], [35, 40]],
]).then(function() { [['I can\'t see a thing ...', '... need to turn on the light ...'], [35, 40]]
console.log("asdf"); ]);
})
act1.update = function() { act1.update = function() {
}; };

View file

@ -4,7 +4,7 @@
var isactive = false; var isactive = false;
var show = function(text, position) { var show = function(text, position) {
return new Promise(function(resolve) { return new Promise(function(resolve) {
isactive = true; isActive = true;
var dom = document.getElementById('bubble'); var dom = document.getElementById('bubble');
dom.style.display = ''; dom.style.display = '';
@ -16,10 +16,10 @@
var show = function() { var show = function() {
if (parts.length === 0) { if (parts.length === 0) {
setTimeout(function() { setTimeout(function() {
isactive = false; isActive = false;
dom.style.display = 'none'; dom.style.display = 'none';
return resolve(); return resolve();
}, 4000); }, 2000);
return; return;
} }
dom.innerHTML += parts.shift() + ' '; dom.innerHTML += parts.shift() + ' ';
@ -30,26 +30,18 @@
}; };
bubble.talk = function(texts, position) { bubble.talk = function(texts, position) {
return new Promise(function(resolve) { if (texts.length === 0) return;
if (texts.length === 0) { var text = texts.shift();
return resolve(); return show(text, position || [5, 40]).then(function() {
} return bubble.talk(texts, position);
var text = texts.shift();
show(text, position || [5, 40]).then(function() {
bubble.talk(texts, position);
});
}); });
}; };
bubble.story = function(talkList) { bubble.story = function(talkList) {
return new Promise(function(resolve, reject) { if (talkList.length === 0) return;
if (talkList.length === 0) { var params = talkList.shift();
return resolve(); return bubble.talk(params[0], params[1]).then(function() {
} return bubble.story(talkList);
var params = talkList.shift();
bubble.talk(params[0], params[1]).then(function() {
bubble.story(talkList);
});
}); });
}; };