AsyncFunction and GeneratorFunction issues
See original GitHub issueI think, tests should pass:
const type = require('type-detect');
const assert = require('assert');
const asyncFn = async () => {};
const generatorFn = function* generator () {};
function getTag(obj) {
return Object.prototype.toString.call(obj).slice(8, -1);
}
assert(type(asyncFn) === getTag(asyncFn));
assert(type(generatorFn) === getTag(generatorFn));
Is it a bug? If not, you should add it to README.
Issue Analytics
- State:
- Created 6 years ago
- Comments:5 (5 by maintainers)
Top Results From Across the Web
async function* - JavaScript - MDN Web Docs - Mozilla
Declaring an async generator function. Async generator functions always produce promises of results — even when each yield step is synchronous.
Read more >Javascript Generator Yield/Next vs Async-Await Overview and ...
An async function cannot wait for multiple promises at the same time. 6. Performance issues can occur if using many await as one...
Read more >How to throw an error in an async generator function in ...
async function* function_name () { ... } We will learn about the declaration of the async generator function and know how to throw...
Read more >Async Generator Functions in JavaScript - The Code Barbarian
The cleanest way to loop through an entire async generator function is using a for/await/of loop. 'use strict'; async function* run() ...
Read more >A look at generator functions and asynchronous generators in ...
A generator function (ECMAScript 2015) in JavaScript is a special type ... Now, the problem with our custom iterable is that it can't...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Hey @doasync thanks for the issue
This, again, is because of the early
typeof
optimisation;async () = >{}
andfunction *() {}
are alltypeof x === "function"
, and so we return that early:https://github.com/chaijs/type-detect/blob/c7895e499ecc0a93c567adef9fa2b33eec13d2ab/index.js#L54-L57
We could probably change that to:
We’d welcome a PR, which includes that change plus you’ll need to change the tests for generatorfunction, which are here: https://github.com/chaijs/type-detect/blob/c7895e499ecc0a93c567adef9fa2b33eec13d2ab/test/new-ecmascript-types.js#L129-L135
Also, on top of that, this lib does not have tests for async functions as they weren’t around when the lib was written - so you’ll need to add tests for that.
I would argue that
type-detect
s role is really a fancy polyfill for@@toStringTag
. Almost all of the code that isn’t returning just[Symbol.toStringTag]
is for performance or for conformance - arguably that includes thetypeof
check at the beginning. Framed this way, it seems to make a lot more sense fortype-detect
to distinguish between different types of functions.