te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - Catching errors from nested asyncawait functions - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Catching errors from nested asyncawait functions - Stack Overflow

programmeradmin2浏览0评论

I have a function chain in a node 4.3 script that looks something like, callback -> promise -> async/await -> async/await -> async/await

like so:

const topLevel = (resolve, reject) => {
    const foo = doThing(data)
    .then(results => {
        resolve(results)
    })
    .catch(err => {
        reject(err)
    })
}

async function doThing(data) {
    const thing = await doAnotherThing(data)
    return thing
}

async function doAnotherThing(data) {
    const thingDone = await etcFunction(data)
    return thingDone
}

(The reason it isn't async/await all the way through is that the top level function is a task queue library, and ostensibly can't be run async/await style)

If etcFunction() throws, does the error bubble up all the way to the top-level Promise?

If not, how can I bubble-up errors? Do I need to wrap each await in a try/catch and throw from there, like so?

async function doAnotherThing(data) {
   try {
     await etcFunction(data)
   } catch(err) {
     throw err  
   }
}

I have a function chain in a node 4.3 script that looks something like, callback -> promise -> async/await -> async/await -> async/await

like so:

const topLevel = (resolve, reject) => {
    const foo = doThing(data)
    .then(results => {
        resolve(results)
    })
    .catch(err => {
        reject(err)
    })
}

async function doThing(data) {
    const thing = await doAnotherThing(data)
    return thing
}

async function doAnotherThing(data) {
    const thingDone = await etcFunction(data)
    return thingDone
}

(The reason it isn't async/await all the way through is that the top level function is a task queue library, and ostensibly can't be run async/await style)

If etcFunction() throws, does the error bubble up all the way to the top-level Promise?

If not, how can I bubble-up errors? Do I need to wrap each await in a try/catch and throw from there, like so?

async function doAnotherThing(data) {
   try {
     await etcFunction(data)
   } catch(err) {
     throw err  
   }
}
Share Improve this question edited Nov 30, 2016 at 15:12 Felix Kling 817k180 gold badges1.1k silver badges1.2k bronze badges asked Nov 28, 2016 at 16:37 BrandonBrandon 7,96610 gold badges53 silver badges75 bronze badges 7
  • What is makePromise? – Bergi Commented Nov 28, 2016 at 16:51
  • "the top level function is a task queue library, and ostensibly can't be run async/await style" - there's no reason why a queue library couldn't use async functions as tasks. You really shouldn't have to deal with callbacks. If you need to use a particular queue library that employs callback style, use a wrapper function. – Bergi Commented Nov 28, 2016 at 16:53
  • @Bergi makePromise is actually an async function, but since it's called from a non-async environment, i'm handling it like a promise. re: i agree, I'm going to try and promise-ify it eventually, but can I have reliable error bubbling in the mean time? – Brandon Commented Nov 28, 2016 at 16:58
  • @Bergi i removed the makePromise bit from the snippet to clarify--the first function called in the top-level is an async function i'm treating as a regular Promise – Brandon Commented Nov 28, 2016 at 17:00
  • 1 @Bergi yes, thanks for catching that & the other errors- i should've just copied the actual code instead of trying to recreate in pseudocode – Brandon Commented Nov 28, 2016 at 17:04
 |  Show 2 more ments

3 Answers 3

Reset to default 7

If etcFunction() throws, does the error bubble up all the way through the async functions?

Yes. The promise returned by the outermost function will be rejected. There's no need to do try { … } catch(e) { throw e; }, that's just as pointless as it would be in synchronous code.

… bubble up all the way to the top-level Promise?

No. Your topLevel contains multiple mistakes. If you don't return the doThing(data) from the then callback, it will be ignored (not even awaited) and the rejection stays unhandled. You'll have to use

.then(data => { return doThing(data); })
// or
.then(data => doThing(data))
// or just
.then(doThing) // remended

And in general, your function should look like this:

function toplevel(onsuccess, onerror) {
    makePromise()
    .then(doThing)
    .then(onsuccess, onerror);
}

No unnecessary function expressions, no .then(…).catch(…) antipattern (that could lead to onsuccess and onerror to both be called).

I know this question is old, but as your question is written, wouldn't the doAnotherThing() function be unnecessary because it just wraps etcFunction()?

So your code could be simplified to this:

async function topLevel(){
  let thing = await doThing(data)
  let thingDone = await etcFunction(data)
  //Everything is done here...
}

//Everything starts here...
topLevel()

I just had a similar issue where my nested errors didn't appear to bubble up to my top level function.

The fix for me was to remove the "try / catch" from my nested function and allow the error to just be thrown.

发布评论

评论列表(0)

  1. 暂无评论