2016-11-18 20 views
1

私は、その約束の機能の外から、約束の中に投げ込まれている例外を捕まえることができるようにしたい。私は、内部エラーをキャッチすることができますが、私は途中でキャッチを追加することによって、私は(最終的に)したい場所に終わるためにそれを投げることができない内部例外をキャッチするにはどうすればよいですか?

throwError =() => { 
    throw new Error("myError"); 
}; 


let iWantToCatchTheErrorThatThisOneThrows =() => { 
    return (new Promise((resolve) => { 
     resolve() 

    }).then(() => { 
     (new Promise((resolve, reject) => { 
      return throwError(); 
     })); 
     return new Promise((resolve) => setTimeout(resolve, 1000)); 
    })).catch((error) => { 
     //Never reaches here 
     console.log(error) 
    }); 
}; 

iWantToCatchTheErrorThatThisOneThrows().then(() => { 
    console.log("Quit gracefully"); 
}).catch((error) => { 
    //Never reaches here 
    console.log(error); 
}); 

を次のコードを参照してください。
let iWantToCatchTheErrorThatThisOneThrows =() => { 
    return (new Promise((resolve) => { 
     resolve() 

    }).then(() => { 
     (new Promise((resolve, reject) => { 
      return throwError(); 
     })).catch((error) => { 
      //Prints, but thrown error isn't bubbled anywhere 
      console.log(error); 
      throw new Error(error); 
     }); 
     return new Promise((resolve) => setTimeout(resolve, 1000)); 
    })).catch((error) => { 
     //Never reaches here 
     console.log(error) 
    }); 
}; 

iWantToCatchTheErrorThatThisOneThrows().then(() => { 
    console.log("Quit gracefully"); 
}).catch((error) => { 
    //Never reaches here 
    console.log(error); 
}); 

すべての試験はNodejs 7.1.0に

+0

内部コールバックでは、あらゆる種類の投げ込みと再投げ込みを行い、無関係な解決済みの約束を返します。外側の '.catch()'にキャッチするものがないことは驚くべきことではありません。 –

答えて

0

を私はすでに解決された約束を拒否したいので、私の質問は、不合理であることに気づきました。問題は、私が約束からすぐに進み、解決/投げを待つことではないということです。より明確な例は、次のようになります。

writeToDatabase =() => { 
    return new Promise((resolve, reject) => { 
     setTimeout(() => { 
      if (hasPermissions) { 
       resolve() 
      } else { 
       reject(); 
      } 
     }, 1000) 
    }); 
}; 


let iWantToCatchTheErrorThatThisOneThrows =() => { 
    return new Promise((resolve) => { 
     resolve() 
    }).then(() => { 
     //Don't want to wait for this to complete 
     //but want to catch the error in later catch clauses 
     //Unfortunately, this is unreasonable since I can't reject 
     //a promise that might already have been resolved. 
     //Therefore, refactor to have the same method to handle error 
     writeToDatabase().catch(handleError); 

     return Promise.resolve(); 
    }).catch((error) => { 
     //Will not catch the database error, since it's already resolved 
     console.log('err', error) 
    }); 
}; 

iWantToCatchTheErrorThatThisOneThrows().then(() => { 
    console.log("Quit gracefully"); 
}).catch((error) => { 
    // Will not catch the database error, since it's already resolved 
    // Instead, we can reuse the same error handling function 
    handleError(error); 
}); 
1

編集し、次のようにあなたのコードを実行されています -

throwError =() => { 
throw new Error("myError"); 
}; 


let iWantToCatchTheErrorThatThisOneThrows =() => { 
    return new Promise((resolve) => { 
     resolve() 
    }).then(() => { 
     //since you are resolving promise , this will be called 
     return Promise.reject('err') 
     // returning rejection so we can catch later 
    }).catch((error) => { 
      //It will not be called unless some error is thrown 
      console.log('err',error) 
    }); 
}; 

iWantToCatchTheErrorThatThisOneThrows().then(() => { 
    console.log("Quit gracefully"); 
    }).catch((error) => { 
    // Since a rejection is returned by function , it is called 
    console.log(error); 
}); 

それともトンをしたい場合にOエラーをスロー: -

throwError =() => { 
    throw new Error("myError"); 
}; 

let iWantToCatchTheErrorThatThisOneThrows =() => { 
    return new Promise((resolve) => { 
    resolve() 

    }).then(() => { 

    return throwError(); 

    }).catch((error) => { 
    //Reaches 
    console.log('err',error) 
    //Rejection returned to catch later 
    return Promise.reject('rejection from catch') 
    }); 
}; 

iWantToCatchTheErrorThatThisOneThrows().then(() => { 
    console.log("Quit gracefully"); 
}).catch((error) => { 
    //rejection caught 
    console.log(error); 
}); 
+0

私のユースケースには完全には適用されません。これは、エラーをスローする約束を待つ必要があるためです – Karamell

関連する問題