Title: Using for-await-of should resolve the Promise? · Issue #21115 · microsoft/TypeScript · GitHub
Open Graph Title: Using for-await-of should resolve the Promise? · Issue #21115 · microsoft/TypeScript
X Title: Using for-await-of should resolve the Promise? · Issue #21115 · microsoft/TypeScript
Description: TypeScript Version: typescript@2.6.2 typescript@2.7.0-dev.20180110 Code declare function require(name:string); (
Open Graph Description: TypeScript Version: typescript@2.6.2 typescript@2.7.0-dev.20180110 Code declare function require(name:string); (
X Description: TypeScript Version: typescript@2.6.2 typescript@2.7.0-dev.20180110 Code declare function require(name:string); (<any> Symbol).asyncIterator = Symbol.asyncIterator || Symbol.for("Symbol.a...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/21115
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Using for-await-of should resolve the Promise?","articleBody":"**TypeScript Version:**\r\n- typescript@2.6.2\r\n- typescript@2.7.0-dev.20180110\r\n\r\n**Code**\r\n\r\n```ts\r\ndeclare function require(name:string);\r\n\r\n(\u003cany> Symbol).asyncIterator = Symbol.asyncIterator || Symbol.for(\"Symbol.asyncIterator\");\r\n\r\nconst fetchlike = function(url) {\r\n // return new pending promise\r\n return new Promise((resolve, reject) => {\r\n // select http or https module, depending on reqested url\r\n const lib = url.startsWith('https') ? require('https') : require('http');\r\n const request = lib.get(url, (response) => {\r\n // handle http errors\r\n if (response.statusCode \u003c 200 || response.statusCode > 299) {\r\n reject(new Error('Failed to load page, status code: ' + response.statusCode));\r\n }\r\n // temporary data holder\r\n const body = [];\r\n // on every content chunk, push it to the data array\r\n response.on('data', (chunk) => body.push(chunk));\r\n // we are done, resolve promise with those joined chunks\r\n response.on('end', () => resolve(body.join('')));\r\n });\r\n // handle connection errors of the request\r\n request.on('error', (err) => reject(err))\r\n })\r\n};\r\n\r\nasync function example() {\r\n const arrayOfFetchPromises = [\r\n fetchlike('http://example.com'),\r\n fetchlike('http://example.com'),\r\n ];\r\n\r\n // Regular iterator:\r\n for (const item of arrayOfFetchPromises) {\r\n console.log(item); // Logs a promise\r\n }\r\n\r\n // for-await-of iterator: \r\n for await (const item of arrayOfFetchPromises) {\r\n console.log(item); // Logs a response\r\n }\r\n}\r\n\r\nexample()\r\n```\r\n\r\n`tsc --lib es2017,esnext.asynciterable,dom Untitled-1.ts && node Untitled-1.js`\r\n\r\n**Actual behavior:**\r\n\r\n`item` is a Promise in the for-await-of iterator block\r\n\r\n\r\n\r\nDoing this causes error:\r\n\r\n```ts\r\n for await (const item of arrayOfFetchPromises) {\r\n console.log(item.length); // TS error\r\n }\r\n```\r\n\r\n```ts\r\n for await (const item of arrayOfFetchPromises) {\r\n console.log((item as any).length);\r\n // not error because typecasted to any, logs valid result\r\n }\r\n```\r\n\r\n**Expected behavior:**\r\n\r\nIn the for-await-of iterator block,`item` should not be a Promise, because it's the actual value instead of a Promise\r\n","author":{"url":"https://github.com/luthfianto","@type":"Person","name":"luthfianto"},"datePublished":"2018-01-10T11:38:12Z","url":"https://github.com/microsoft/TypeScript/issues/21115"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:90d77606-8127-885c-793c-08fdcd3de5bf |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | E734:3F8A67:1EED3FE:2948D02:6B017BC0 |
| html-safe-nonce | 0fd60ee597dea38c059bf4651ea881f6118857ac703af09198e57855eb3c9531 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFNzM0OjNGOEE2NzoxRUVEM0ZFOjI5NDhEMDI6NkIwMTdCQzAiLCJ2aXNpdG9yX2lkIjoiMTY5NDk2MjYzOTkwMzQ4ODk2MCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | be66d8173b8da9f7ffa17714bb7f4b02b6a5f861514b45192263360c17ce9a81 |
| hovercard-subject-tag | issue:287403528 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/microsoft/TypeScript/21115/issue_layout |
| twitter:image | https://opengraph.githubassets.com/c9bc888f521c24081febb18a7b04654bf3cb0c31c3e0e4a6fa75458333f87741/microsoft/TypeScript/issues/21115 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/c9bc888f521c24081febb18a7b04654bf3cb0c31c3e0e4a6fa75458333f87741/microsoft/TypeScript/issues/21115 |
| og:image:alt | TypeScript Version: typescript@2.6.2 typescript@2.7.0-dev.20180110 Code declare function require(name:string); ( |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | luthfianto |
| hostname | github.com |
| expected-hostname | github.com |
| None | cc5dfdd32f62ebd3223cab3af3aa2b268d3605218237326a48a99d1efd89495c |
| turbo-cache-control | no-preview |
| go-import | github.com/microsoft/TypeScript git https://github.com/microsoft/TypeScript.git |
| octolytics-dimension-user_id | 6154722 |
| octolytics-dimension-user_login | microsoft |
| octolytics-dimension-repository_id | 20929025 |
| octolytics-dimension-repository_nwo | microsoft/TypeScript |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 20929025 |
| octolytics-dimension-repository_network_root_nwo | microsoft/TypeScript |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 8c77c9e8439031ad54c75a250694842892f693e1 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width