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')
.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() {
};

View file

@ -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);
});
};