Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add onFirstPause utility method #2

Closed

Conversation

nicolo-ribaudo
Copy link
Contributor

async: function([item, cb], resolve, reject) {
evaluateAsync(item, resolve, reject, cb);
},
}),
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can do what you want with something like this?

  async: function([item, cb], resolve, reject) {
    let completed = false;

    evaluateAsync(item, value => {
      completed = true;
      resolve(value);
    }, err => {
      completed = true;
      reject(value);
    });

    if (!completed) {
      cb();
    }
  },

or alternatively using the existing public API:

const runGenerator = gensync(function* (item) {
  return yield* item;
});

const onFirstPause = gensync({
  name: "onFirstPause",
  arity: 2,
  sync: function([item]) {
    return runGenerator.sync(item);
  },
  errback: function([item, firstPause], cb) {
    let completed = false;

    // Gensync calls `errback` callbacks synchronously if it can, even in the async case, if I remember right
    runGenerator.errback(item, (err, value) => {
      completed = true;
      cb(value);
    });

    if (!completed) {
      firstPause();
    }
  },
})

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I will try the second approach 👍

@nicolo-ribaudo
Copy link
Contributor Author

babel/babel@cc2cfd7

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants