@Holfamer

Как вернуть значение из функции, где есть промисы?

Код:
function getKmzFileAsText() {
			    var text = '';
                var xhr = new XMLHttpRequest(),
                    fileReader = new FileReader();

                xhr.open("GET", "kmz/1.kmz", true);
                // Set the responseType to blob
                xhr.responseType = "blob";

                xhr.addEventListener("load", function () {
                    if (xhr.status === 200) {
                        // Load blob as Data URL
                        fileReader.readAsDataURL(xhr.response);
                        JSZip.loadAsync(xhr.response).then(function (content) {
                            // if you return a promise in a "then", you will chain the two promises
                            return content.files["doc.kml"].async('text');
                        })
							.then(function (txt) {
                            alert(txt);
                            text = String(txt);
                        });
                    }
                }, false);
                // Send XHR
                xhr.send();
                console.log(text);
//                return text;
            }
  • Вопрос задан
  • 243 просмотра
Пригласить эксперта
Ответы на вопрос 3
function getKmzFileAsText(callback) {
  var text = '';
  var xhr = new XMLHttpRequest(),
      fileReader = new FileReader();

  xhr.open("GET", "kmz/1.kmz", true);
  // Set the responseType to blob
  xhr.responseType = "blob";

  xhr.addEventListener("load", function () {
    if (xhr.status === 200) {
      // Load blob as Data URL
      fileReader.readAsDataURL(xhr.response);
      JSZip.loadAsync(xhr.response)
      .then(function (content) {
         // if you return a promise in a "then", you will chain the two promises
         return content.files["doc.kml"].async('text');
      })
     .then(function (txt) {
       alert(txt);
       text = String(txt);
       return callback(text);
     });
   }
 }, false);
  // Send XHR
  xhr.send();
  console.log(text);
}

getKmzFileAsText(function(result) {
	var text = result
});
Ответ написан
Комментировать
abyrkov
@abyrkov
JavaScripter
Ответ написан
Комментировать
DIITHiTech
@DIITHiTech
Fullstack javascript developer
function getKmzFileAsText() {

    return new Promise(function(resolve, reject){
        var text = '';
        var xhr = new XMLHttpRequest(),
            fileReader = new FileReader();

        xhr.open("GET", "kmz/1.kmz", true);
        xhr.responseType = "blob";

        xhr.addEventListener("load", function () {
            if (xhr.status === 200) {
                // Load blob as Data URL
                fileReader.readAsDataURL(xhr.response);
                JSZip.loadAsync(xhr.response)
                    .then(function (content) {
                        // if you return a promise in a "then", you will chain the two promises
                        resolve(content.files["doc.kml"].async('text'));
                    }).then(resolve, reject);
            }else{
                reject(Error(xhr.status))
            }
        }, false);

        xhr.ontimeout= xhr.onerror= reject;

        xhr.send();
    });
}

getKmzFileAsText().then(function(result){
    console.log(result);
});
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы