{"action":"opened","issue":{"url":"https://api.github.com/repos/caunion/ttthon/issues/1","repository_url":"https://api.github.com/repos/caunion/ttthon","labels_url":"https://api.github.com/repos/caunion/ttthon/issues/1/labels{/name}","comments_url":"https://api.github.com/repos/caunion/ttthon/issues/1/comments","events_url":"https://api.github.com/repos/caunion/ttthon/issues/1/events","html_url":"https://github.com/caunion/ttthon/issues/1","id":326256963,"number":1,"title":"5/23 Review","user":{"login":"caunion","id":2020324,"avatar_url":"https://avatars2.githubusercontent.com/u/2020324?v=4","gravatar_id":"","url":"https://api.github.com/users/caunion","html_url":"https://github.com/caunion","followers_url":"https://api.github.com/users/caunion/followers","following_url":"https://api.github.com/users/caunion/following{/other_user}","gists_url":"https://api.github.com/users/caunion/gists{/gist_id}","starred_url":"https://api.github.com/users/caunion/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/caunion/subscriptions","organizations_url":"https://api.github.com/users/caunion/orgs","repos_url":"https://api.github.com/users/caunion/repos","events_url":"https://api.github.com/users/caunion/events{/privacy}","received_events_url":"https://api.github.com/users/caunion/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2018-05-24T19:21:33Z","updated_at":"2018-05-24T19:21:33Z","closed_at":null,"author_association":"OWNER","body":"昨天大家的任务完成情况和我预想得有些差距。主要问题包括：\r\n\r\n1. 大家的task1_1.js 的promise用法还是callback的思路，层层嵌套。事实上应该用Promise Chain来写：\r\n```\r\ngetBalance()\r\nthen(getOrderbook)\r\n.then(createOrders)\r\n.then(promise_wait)\r\n.then(getOrderStatus)\r\n.then(cancelOrders)\r\n```\r\n这样的写封装性也好一些。\r\n\r\n2. 基本任务要求**都用Promise**。这个要求写的很明确了。但是还有不少同学提交的内容用的`async/await`写的。我们要求的是拓展部分才是用`async/await`。\r\n\r\n3. 大部分人的task1_2.js 采用了一个比较讨巧的方法，最后process.exit()；而不是让程序自然退出。这是因为有的同学的没有检查是否已经timeout，于是递归的部分会继续运行。\r\nstep1本意是实现一个 retry promise；step2的本意是实现一个retry promise with timeout。\r\n完全可以单独写一个函数`retryPromise`。\r\n```\r\n// func is a function that returns a promise\r\n// param is parameters array for func\r\n// predicate is predicate function indicate if we should retry\r\n\r\nfunction retryPromise(func, param, predicate) {\r\n   return new Promise((resolve, reject) => {\r\n      const ret = await func(...param)\r\n      if (predicate(ret) === true) {\r\n         return retryPromise(func, param, predicate)\r\n      } else return ret\r\n  })\r\n}\r\n\r\n// usage:\r\n// func: GetRandomNumber\r\n// param: []\r\n// predicate: (num) => num > 80 && !isTimeout\r\n```\r\n这样以来，step2也可以重用这段代码。\r\n\r\n在Step2，为了实现timeout，可以先写一个promise-timeout\r\n```\r\nfunction timeoutPromise (time = 3000) {\r\n  return new Promise((resolve, reject) => {\r\n    setTimeout(resolve, time)\r\n    isTimeout = true\r\n  })\r\n}\r\n```\r\n然后，利用`retryPromise`和`Promise.race`就可以简洁实现Step2：\r\n```\r\n// step2 should also return a promise\r\nfunction Step2() {\r\n return Promise.race([\r\n    retryPromise(func, param, predicate),\r\n    timeoutPromise()\r\n   ])\r\n}\r\n```\r\n\r\n这样，最后的`Step1(), Step2(), Step3()`都可以用Promise chain连在一起：\r\n```\r\nfunction main() {\r\n  Step1()\r\n  .then(Step2)\r\n  .then(Step3)\r\n} \r\n```\r\n这里必须要说明，昨天hank同学使用了bluebird的timeout()方法，直接用在PromiseChain上，这是我之前没有想到的。这种用法很有意思，大家可以学习。这也正是我们让大家使用bluebird promise，而不是native promise的原因：bluebird的promise更加强大！\r\n关于bluebird timeout的用法：\r\nhttp://bluebirdjs.com/docs/api/timeout.html\r\n\r\n4. 还有一些小问题，大家需要注意：\r\n  a. 每天的练习任务在子目录下，比如`your_repo/task1`。请不要把当天需要用的文件放到除此以外的位置。\r\n  b. 请在你的任务子目录下包含`package.json`，否则我无法知道你用了哪些package，我也无法运行你的代码。\r\n  c. 请.gitignore掉.idea, node_modules等目录。\r\n  d. 请在任务自目录下包含`README.md`。请用 markdown 语法写。提交之后请到自己的github页面去查看是否可读(rtf不是markdown)。\r\n\r\n5. 关于promise的更多妙用，请查看\r\nhttps://github.com/sindresorhus/promise-fun"}}
{"actor":{"display_login":"caunion"}}