DEV Community

YCM Jason
YCM Jason

Posted on

2 1

My attempt on asyncToGenerator()

Not gonna explain so much, just sharing my recent attempt on implementing asyncToGenerator(). Please do tell me what you think. πŸ˜€

function asyncToGenerator(fn) {
  const ensurePromise = v => Promise.resolve(v);

  const stepContext = (context, nextOrThrow, prev) => {
    const { value, done } = context[nextOrThrow](prev);

    if (done) return ensurePromise(value);

    return ensurePromise(value)
      .then(v => stepContext(context, 'next', v))
      .catch(err => stepContext(context, 'throw', err));
  };

  return function(...args) {
    const context = fn.apply(this, args); 
    return stepContext(context, 'next');
  };
}
Enter fullscreen mode Exit fullscreen mode

To use:

asyncToGenerator(function* () {
  const res = yield axios.get('https://www.ycmjason.com');
  console.log(res);
})();
Enter fullscreen mode Exit fullscreen mode

Sentry blog image

How to reduce TTFB

In the past few years in the web dev world, we’ve seen a significant push towards rendering our websites on the server. Doing so is better for SEO and performs better on low-powered devices, but one thing we had to sacrifice is TTFB.

In this article, we’ll see how we can identify what makes our TTFB high so we can fix it.

Read more

Top comments (2)

Collapse
 
mrm8488 profile image
Manuel Romero β€’

I created a module to do something similar: github.com/mrm8488/asyncflow

Collapse
 
ycmjason profile image
YCM Jason β€’

Nice work!

It's just a proof of concept I guess. We all use async/await nowadays! But it's really fun to try to write this from scratch.

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

πŸ‘‹ Kindness is contagious

Please leave a ❀️ or a friendly comment on this post if you found it helpful!

Okay