2017-03-28 153 views
0

我有此方法fallbackToLocalDBfileOrLocalStorageDB其返回一个承诺和调用另一个方法getDBfileXHR这也是一个承诺。嵌套承诺打字稿角2

在这段代码中,我想知道它,我必须使用'resolve()'或者如果解析getDBfileXHR会自动解决fallbackToLocalDBfileOrLocalStorageDB

正如你可以看到我的评论部分then().catch(),但我不知道如果我不得不离开它们。

感谢您的帮助。

fallbackToLocalDBfileOrLocalStorageDB() { 
     return new Promise(function (resolve, reject) { 
      if (this.storageService.get('prodata') === null) { 
       if (this.connectionStatus.f() !== 'online') { 
       } else { 
        this.sendErrorEmail("BL: online but falling back to local proDB", 10); 
       } 
       console.log('...falling back to local proBD.jsonp.'); 
       return this.getDBfileXHR('proDB.jsonp'); 
        // .then(function() { 
        // console.log('...falling back to local proBD.jsonp succeeded.'); 
        // resolve(); 
        // }) 
        // .catch(, function() { 
        // console.log('...error, shit.'); 
        // reject(); 
        // }); 

EDIT表示完全嵌套的功能,具有部分固定的代码:

import { Injectable } from '@angular/core'; 
... 

export class UpdateProDB { 

    constructor(
     ) { 
    } 


    get() { 
     var debugOptionUseLocalDB = 0, 
     prodata = [], 
     serverAttempts = 0;  return new Promise((resolve, reject) => { 
      if (debugOptionUseLocalDB) { 
       return this.fallbackToLocalDBfileOrLocalStorageDB(); 
      } 
      if (this.connectionStatus.f() === 'online') { 
       console.log("Fetching DB from the server:"); 
       return this.getDBfileXHR(this.dbUrl(), serverAttempts) 
       .then(function (data) { 
        console.log('-normal XHR request succeeded.'); 

        resolve(data); 
       }) 
       .catch((reason)=> { 
        if (typeof serverAttempts !== "undefined") serverAttempts++; 
        console.log('on passe dans le catch, serverAttempts = ', serverAttempts) 
        if (serverAttempts < 2) { 
         return this.getDBfileXHR(this.dbUrl(), serverAttempts) 
         .then(function() { 
          console.log('-basic XHR request succeeded.'); 

         }) 
         .catch(function(){ 
          console.log("-basic XHR request failed, falling back to local DB file or localStorage DB..."); 

         }) 
        } else { 
         console.log("-basic XHR request failed, falling back to local DB file or localStorage DB..."); 
         return this.fallbackToLocalDBfileOrLocalStorageDB() 
         .then((data)=>{ 
          resolve(data); 
         }) 
         .catch((reason)=> { 
          reject(reason); 
         }); 
        } 
       }); 
      }); 
    } 

    getDBfileXHR(url, serverAttempts) { 
     return new Promise((resolve, reject) => { 
      var request = new XMLHttpRequest(); 
      request.open("GET", url, true);    
      request.onload =()=> { 
       if ((request.readyState === 4) && ((request.status >= 200 && request.status <= 299) || request.status === 304 || request.status === 0)) { 
        console.log('-we get response '+request.status+' from XHR in getDBfileXHR'); 

        var jsonText = request.responseText.replace("callback(", "").replace(");", ""); 

        if (jsonText === '') { 
         console.error('-error'); 

         reject({ 
          status: request.status, 
          statusText: request.statusText 
         }); 

        } else { 
         var parsedJson; 
         try { 
          parsedJson = JSON.parse(jsonText); 
         } catch (e) { 
          resolve(request.response); 
         } 
        } 
       }; 
       request.onerror =()=> { 
        reject({ 
         status: request.status, 
         statusText: request.statusText 
        }); 
       }; 
       console.log("sending request.send()"); 
       request.send(); 

      }); 
    } 


    fallbackToLocalDBfileOrLocalStorageDB() { 
     return new Promise((resolve, reject) => { 
      if (this.storageService.get('prodata') === null) { 
       if (this.connectionStatus.f() !== 'online') { 

       } else { 
        this.sendErrorEmail("BL: online but falling back to local proDB", 10); 
       } 
       console.log('...falling back to local proBD.jsonp.'); 
       return this.getDBfileXHR('proDB.jsonp', undefined) 
       .then(function (data) { 
        console.log('...falling back to local proBD.jsonp succeeded.'); 
        resolve(data); 
       }) 
       .catch((reason)=> { 
        console.log('...error, shit.'); 
        reject(reason); 
       }); 
      } else { 
       resolve(); 
      } 
     }); 
    } 

回答

1

new Promise()的参数被称为执行程序功能。它需要resolve()reject()的承诺。

你要做的是从该执行器函数中返回另一个Promise。根据MDN,“执行者的回报值被忽略。”

这意味着你想的方式来使用这个内部Promise将无法​​正常工作,你必须明确解决或者拒绝你是在为你的注释代码做的事情。

+0

我部分固定的代码都与你的答案..但我认为我还不在那里!你认为什么? – Louis

+0

它在做什么?它没有做什么? –

+0

我甚至没有运行它!我刚才检查了脚本错误;) – Louis

1

首次使用箭头功能=>代替function保持this关键字:

fallbackToLocalDBfileOrLocalStorageDB() { 
     return new Promise((resolve, reject) => { 
      if (this.storageService.get('prodata') === null) { 
       if (this.connectionStatus.f() !== 'online') { 
       } else { 
        this.sendErrorEmail("BL: online but falling back to local proDB", 10); 
       } 
       console.log('...falling back to local proBD.jsonp.'); 
       return this.getDBfileXHR('proDB.jsonp'); 
         .then(function (data) { 
         console.log('...falling back to local proBD.jsonp succeeded.'); 
         resolve(data); 
        }) 
         .catch((reason)=> { 
         console.log('...error, shit.'); 
         reject(reason); 
         }); 

和郁可访问您喜欢这样的数据:

fallbackToLocalDBfileOrLocalStorageDB().then((data)=>{ 
    console.log(data); 
}) 
+0

我部分固定的代码(编辑问题)......但我现在还没有,我认为;)正如你可以看到我嵌套几个诺言...... – Louis

+0

请分开你的逻辑,导致你的代码是不可维护 –

+0

做什么你的意思是@BougarfaouiElHoucine?你能给个例子吗 ? – Louis