2017-01-09 19 views
0

私は角度2のコードで間違っていることを解決できません。私の約束は正しい結果を返さない。プロミスが解決しない解決

私のコードは次のようになります。this.addPlan(...)は、サーバログと呼ばれる

this.addPlan('My plan title9', "YES9") 
 
    .then((id)=>{ 
 
     console.log('Promise return was: ' + id); 
 
    }) 
 
    .catch((err)=>{ 
 
     console.log('Call to addPlan failed with err = ' + err); 
 
    }); 
 

 
    addPlan(title, security) 
 
    { 
 
    let timeStamp \t = new Date().toISOString(); 
 
    let plan \t \t = { 
 
     _id \t \t : 'PLAN:' + timeStamp, 
 
     title \t \t : title, 
 
     security \t : security, 
 
     notes  : [],   
 
     flags  : [],   
 
     created : timeStamp, 
 
     updated \t : timeStamp 
 
     }; 
 

 
    return new Promise(resolve => 
 
    { 
 
     var theID; 
 
     this._DB.put(plan) 
 
     .then(function (response) { 
 
     console.log(JSON.stringify(response)); 
 
     resolve(response.id); 
 
     theID = response.id; 
 
     }) 
 
     .catch((err) => 
 
     { 
 
     console.log('addPlan error is: ' + err); 
 
     this.success = false; 
 
     }); 
 

 
     if(this.success) 
 
     { 
 
     this.handleSyncing(); 
 
     resolve(theID); 
 
     } 
 

 
    }); 
 
    }

は次のとおりです。

Promise return was: undefined 
{"ok":true,"id":"PLAN:2017-01-09T18:16:50.094Z","rev":"1-ac45a4785982fcbbcb46dd099431ecb6"} 

それがあるべきときの約束からのリターンは定義されていません'id'の値また、コンソールにPromiseメッセージが最初に表示されますが、プロミスが返された後に表示されるはずです。

明らかに私はここで初心者のエラーを作りましたが、私はそれが何かを見ることができません。

答えて

2

非同期コードを同期しているかのように処理しているため、エラーはif(this.success)です。作成した新しい約束のブロック内のすべてが同期して実行されます。

  1. iftrueに評価し、まだ定義されていない 値を解決します:

    出力を見ると、何が起こるかを理解することはかなりまっすぐ進むべきです。

  2. put()関数呼び出しが完了し、応答をコンソールに記録します。

deferred anti-patternも実装しています。 put()関数がすでに1つを返すので、新しい約束事を作成する必要はありません。それを返すだけで、.then()内の応答が返され、これを約束して解決します。以下のコードでthis.handleSyncing();を省略しました。なぜそれが何をしているのか完全にはっきりしていないからです。その後、上の

addPlan(title, security){ 
    let timeStamp = new Date().toISOString(); 
    let plan  = { 
     _id   : 'PLAN:' + timeStamp, 
     title  : title, 
     security : security, 
     notes  : [],   
     flags  : [],   
     created : timeStamp, 
     updated  : timeStamp 
     }; 
    return this._DB.put(plan).then(response => { 
     return response.id 
    }) 
    } 

と応答():あなたが新しい約束

を作成する必要はありません

function addPlan(title, security) { 
    let timeStamp = new Date().toISOString(); 
    let plan = { 
    _id: 'PLAN:' + timeStamp, 
    title: title, 
    security: security, 
    notes: [],   
    flags: [],   
    created: timeStamp, 
    updated: timeStamp 
    }; 

    return this._DB.put(plan) 
    .then((response) => { 
     console.log(JSON.stringify(response)); 
     return response.id; 
    //^^^^^^----- This will wrap the response.id in a promise and will be the resolved value 
    }) 
    .catch((err) => { 
     console.log('addPlan error is: ' + err); 
     this.success = false; 
    }); 
} 
1

あなただけの "this._DB.put(案)" の約束を返すことができます。 id:

this.addPlan('My plan title9', "YES9").then((id)=>{ 
     console.log('Promise return was: ' + id); 
    }) 
関連する問題