将承诺错误解决为高阶承诺可以吗

将承诺错误解决为更高阶的承诺EX可以吗


new Promise(async (resolve, reject) => {

   //call async function


   await async_function(resolve, reject);

})


const async_function = async (resolve, reject) => {

  // some code 

  get_data_from_db

     .then(data => resolve(null,data))

     .catch(e => resolve(e, null)

}

get_data_from_db可以调用其他异步函数。拒绝并没有使错误返回更高的承诺


叮当猫咪
浏览 143回答 2
2回答

慕尼黑8549860

由于同样await适用于非承诺,因此您可以保存整个Promise构造:const async_function = () => get_data_from_db(); (async()=>{    try {        //call async function        const data = await async_function();        console.log(data);    } catch (err) {        console.error(err.message);    }})();

翻翻过去那场雪

可以兑现诺言吗有时候没关系。将被拒绝的承诺视为“异步抛出”和它的障碍但是您的代码是反模式的示例http://bluebirdjs.com/docs/anti-patterns.html#the-explicit-construction-anti-patternconst async_function = async (resolve, reject) => {  // some code   get_data_from_db     .then(data => resolve(null,data))     .catch(e => resolve(e, null)}您可以像这样重写它:const async_function = () =>  get_data_from_db()     .catch(e => e) // it will be resolved with e
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript