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

[wip] POC for await-able Async functions #1526

Closed
wants to merge 2 commits into from
Closed

Conversation

aearly
Copy link
Collaborator

@aearly aearly commented Apr 15, 2018

Related to #1515

This is something I played around with over a year ago.

@@ -25,6 +25,7 @@ import wrapAsync from './internal/wrapAsync'
* If you need the index, use `eachOf`.
* @param {Function} [callback] - A callback which is called when all
* `iteratee` functions have finished, or an error occurs. Invoked with (err).
* @returns {Promise} a promise, if the callback is omitted
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Deliberately never returning anything from an Async function has given us the headroom to do this!

@@ -0,0 +1,24 @@
import noop from 'lodash/noop';

var supportsPromise = typeof Promise === 'function';
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

A pretty crude way to detect promise support. This would be a 3.0 feature. I thinking of dropping support for non-ES2015 environments, in which case we could always return a Promise.

Copy link
Collaborator

Choose a reason for hiding this comment

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

👍 for dropping pre-ES2015 support in v3.

done(err);
});
it('should handle async functions in map', async () => {
var result = await async.map(input, asyncIdentity);
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

😍

Copy link
Collaborator

Choose a reason for hiding this comment

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

This is sweet! It would be cool to also test async with generators, perhaps having a generator function that initializes some data for some of the test cases

it('should handle async functions in each', async () => {
var promise = async.each(input, asyncIdentity);
assert(typeof promise.then === 'function');
assert(Object.getPrototypeOf(promise) === promiseProto);
Copy link

Choose a reason for hiding this comment

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

Why not simpler?

assert(promise instanceof Promise);

@hargasinski
Copy link
Collaborator

This is exciting! Looks good so far, let me know if you need help with this PR though. I could add the rest of the tests/docs.

if (fn === null) return;
var callFn = fn;
fn = null;
callFn.apply(this, arguments);
};
wrapped.promise = fn.promise;
Copy link
Collaborator

@megawac megawac Apr 24, 2018

Choose a reason for hiding this comment

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

Should the wrapped function not return a promise instead to allow once(fn)(...args).then(...) or how do we intend this to work?

@@ -50,5 +50,6 @@ export default function _eachOfLimit(limit) {
}

replenish();
return callback.promise;
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think we'll want to track whether the callback is a user defined callback or an internal promise callback and only return promiseCallback.promise if its an internal version to avoid cases where users pass us a callback with some state on it.

@aearly aearly added this to the 3.0 milestone May 21, 2018
@DanielRamosAcosta
Copy link

DanielRamosAcosta commented May 24, 2018

Hi guys!

I've just saw this PR, and I want to tell you guys I've been developing an alternative version of async with promises. Here is the repo: DanielRamosAcosta/async-promised.

Currently I’ve promisified about 65% of the library functions (with their respective tests), you can see the progress here: https://github.com/DanielRamosAcosta/async-promised/blob/master/TODO.md

My plans were:

  • Implement all the test with async/await/promises
  • Do it just wrapping the original async library
  • Fix docs & TypeScript typings
  • Implement all functions in a promise-native way, so I can drop the original async dependency

But now that I’ve seen this PR, maybe we can do something together! I’ll appreciate any thoughts you share with me.

@aearly
Copy link
Collaborator Author

aearly commented Aug 5, 2018

This branch has diverged greatly from master, I intend to re-implement this feature soon.

@aearly
Copy link
Collaborator Author

aearly commented Aug 5, 2018

Closing in favor of #1572

@aearly aearly closed this Aug 5, 2018
@aearly aearly deleted the DELETE_THIS_BRANCH branch May 19, 2019 02:35
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

5 participants