question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

AsyncFunction and GeneratorFunction issues

See original GitHub issue

I 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:open
  • Created 6 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
keithamuscommented, Aug 11, 2017

Hey @doasync thanks for the issue

This, again, is because of the early typeof optimisation; async () = >{} and function *() {} are all typeof 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:

  var typeofObj = typeof obj;
  if (typeofObj !== 'object' && typeofObj !== 'function') {
    return typeofObj;
  }

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.

0reactions
keithamuscommented, Aug 18, 2017

I would argue that type-detects 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 the typeof check at the beginning. Framed this way, it seems to make a lot more sense for type-detect to distinguish between different types of functions.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found