Javascript链接承诺只处理承诺之一

时间:2017-02-20 23:48:31

标签: javascript promise vuejs2

我有一个Javascript链式承诺,只处理question承诺。注意到progress承诺后,question承诺有效。

我需要更改哪些来处理这两个承诺?

nextQ: function(item){

        var percentQuestion = 100/this.numberQuestions;

        var progress = new Promise(
          function (resolve, reject) {
            console.log(percentQuestion);


          var elem = document.getElementById("myBar");

          var numericWidth = elem.style.width.replace(/\D/g,'');

          var currentWidth = +numericWidth;

          var newWidth = +numericWidth + +percentQuestion;

          var id = setInterval(frame, 10);
          function frame() {
            if (currentWidth >= newWidth) {
              clearInterval(id);
            } else if(currentWidth < 100){
              currentWidth++;
              elem.style.width = currentWidth + '%';
                }
                else{
                  console.log('max');
                };
            };
            resolve();
            }
          );

          var question = new Promise(
            function (resolve, reject) {

              var newKey = item + 1;

              var y = document.getElementById('question' + item);

               var x = document.getElementById('question' + newKey);



                if( y.style.display === 'block'){
                       y.style.display = 'none';
                       x.style.display = 'block';
                       console.log("Changed");
               };
               resolve();
              }
            );

                Promise.all([progress,question]).then(function () {
                    console.log('processed');
                })
                .catch(function (error) {
                    console.log(error.message);
                });
      },

0 个答案:

没有答案