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 - Can pure functions be asynchronous? - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Can pure functions be asynchronous? - Stack Overflow

programmeradmin4浏览0评论

While going through the definition for pure functions, it's generally defined with two traits:

1) Should produce same output given the same input

2) Should not produce any side effects

Does it also imply that a pure function shouldn't be asynchronous? If no, how so? If yes, I would love to see some examples of asynchronous pure function in JavaScript.

While going through the definition for pure functions, it's generally defined with two traits:

1) Should produce same output given the same input

2) Should not produce any side effects

Does it also imply that a pure function shouldn't be asynchronous? If no, how so? If yes, I would love to see some examples of asynchronous pure function in JavaScript.

Share Improve this question asked Oct 6, 2018 at 15:20 Anish K.Anish K. 2,5323 gold badges22 silver badges27 bronze badges 3
  • In your opinion, does new Promise(() => {}) !== new Promise(() => {}) violate trait 1? Other than not being the same object, the promises behave identically. – Patrick Roberts Commented Oct 6, 2018 at 15:31
  • 1 @PatrickRoberts I guess it does violate the rule#1, since technically my outputs won't be the same while calling the same function twice. – Anish K. Commented Oct 6, 2018 at 15:35
  • 2 Disagree - if referential equality is a requirement for purity, then any function that returns an object in javascript could not be considered pure. – dvlsg Commented Oct 6, 2018 at 19:57
Add a ment  | 

2 Answers 2

Reset to default 14

Yes, an asynchronous function usually isn't pure, as it conflicts with requirement #2: no side effects.

Most things that we use asynchronous functions for are inherently side-effectful: I/O, network stuff, timers. But even if we ignore those, promises alone rely on some kind of global state for their asynchrony: the event loop. This typically doesn't fit within our definition of purity.

On the other hand, we can simply ignore those when arguing about purity of our functions, just as we ignore all the low-level effects and timings that a putation has on our real-world machine. If you want to argue that your asynchronous function is pure, you should however always state this assumption explicitly. When arguing about the equivalence of two asynchronous values, you will need to have a sophisticated idea of how you model asynchronous effects, e.g. in the evaluation of Promise.race.

yes, async functions are no different from regular functions except that async means it returns Promise<T> rather just T, with that said, this is the only difference from the sync functions which can be pure, hence async functions can be pure too

example:

async function willBeValue<T>(value: T): Promise<T> { return await value; }
发布评论

评论列表(0)

  1. 暂无评论