2017-08-01 11 views
2

私のモバイルアプリケーションは、接続強度の疑いのある遠隔地から複数のファイルをサーバーに連続してアップロードします。このため、私はファイルを送信しようといくつかの試みをしたいと思います。また、エクスポートの最後にすべてのエラーメッセージが表示されて、エラーが発生した場合は次のファイルを試してみたい(つまり、「10ファイルがアップロードされた、3ファイルが失敗しました...」)Typescriptで成功/失敗するまでプロミスを再試行します

ただし、約束通りの再帰的な再試行パターンを理解することができない。ここでは、私がこれまで持っているものです:私は、サーバー上の例外を発生させた場合

、私は「失敗した3回!!!」表示されますエラーメッセージが表示されますが、呼び出し元の約束は、エクスポートの残りの部分が移動するために解決されません。私はネストされた約束事(すなわち、再試行ごとに新しい約束事を作成する)が作成されているからです。 3回の再試行後に元の約束を解決するにはどうすればよいですか?

ありがとうございます!

+1

は 'this.sendFile(パラメータ、--retries)で返さ' は、代わりにこれを試してみてください: 'this.sendFiles(パラメータ、--retries).resolve(Resolveは、拒否します) ' – Antony

+0

私は約束をやり直す必要があるので、そこに何があったのかを見て、TypeScriptのために何も存在しないことがわかったので、私は自分自身を巻いた。フィードバック歓迎。 https://npmjs.com/package/retry-promise-typescript –

答えて

1

ここで作業を終わったものです:

sendFile(params, retries = 3, promise = null){ 
    console.log("SENDING FILE: ", retries, "attempts remaining", params) 

    if(retries > 0){ 
     return this._sendFile(params) 
     .then(()=>{ 
     // Upload Success 
     console.log("Upload Success!") 
     return Promise.resolve(true) 
     }) 
     .catch((err)=>{ 
     console.log("Upload Fail", err) 

     this.exportStatus.retries++ 

     return this.sendFile(params, --retries) // <-- The important part 
     }) 
    }else{ 
     console.log("Failed 3 times!!!") 

     this.exportStatus.errors.push({ 
     message:"A file failed to upload after 3 attempts.", 
     params: params 
     }) 

     return Promise.resolve(false) 

    } 

    } 
2

あなたは、あなたが同時に取り扱い再試行ロジックを心配する必要はありませんし、どのようなロジックを使用してコードをリファクタリングすることができ、あなたのためにPromise()その自動的チェーンの再試行のためのラッパーを実装することができ。あなたの使い方は次のようになります:以下

sendFile(params, retries = 3) { 
    return Promise.retry(retries, (resolve, reject) => { 
    this._sendFile(params).then(resolve, reject) 
    }) 
} 

をあなたはPromise.retry()を実装する方法です:

Object.defineProperty(Promise, 'retry', { 
 
    configurable: true, 
 
    writable: true, 
 
    value: function retry (retries, executor) { 
 
    console.log(`${retries} retries left!`) 
 

 
    if (typeof retries !== 'number') { 
 
     throw new TypeError('retries is not a number') 
 
    } 
 

 
    const promise = new Promise(executor) 
 

 
    if (retries > 0) { 
 
     return promise.catch(error => Promise.retry(--retries, executor)) 
 
    } 
 

 
    return promise 
 
    } 
 
}) 
 

 
Promise.retry(100, (resolve, reject) => { 
 
    // your sendFile core logic with proper 
 
    // calls to resolve and reject goes here 
 
    const rand = Math.random() 
 

 
    console.log(rand) 
 

 
    if (rand < 0.1) resolve(rand) 
 
    else reject(rand) 
 
}).then(
 
    value => console.log(`resolved: ${value}`), 
 
    error => console.log(`rejected: ${error}`) 
 
)

あなたはネイティブオブジェクトを拡張不快なら(これは次のようになりますこれは、構成可能で、列挙可能ではなく書き込み可能なプロパティなので)、静的関数として実装することができます。

0123あなたが約束して何かをする必要があり
function retry (retries, executor) { 
    console.log(`${retries} retries left!`) 

    if (typeof retries !== 'number') { 
    throw new TypeError('retries is not a number') 
    } 

    const promise = new Promise(executor) 

    if (retries > 0) { 
    return promise.catch(error => retry(--retries, executor)) 
    } 

    return promise 
} 
関連する問題