-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
Promise support #956
Comments
async.waterfall([
function (done) {
// Do something and call done()
fs.readFile(filepath, done);
},
async.asyncify(function(content) {
// Do something and return Promise
return mongoose.models.file.create({
name: filepath,
content: content
});
})
], (err, model) => {
if (err) {
console.error(err);
}
}); But that feature isn't documented... |
I also had another thought -- should we automatically asyncify functions that return promises (or handle them appropriately?). What should async do when passed an ES-whatever e.g. async.waterfall([
function (done) {
// Do something and call done()
fs.readFile(filepath, done);
},
async function(content) {
return await mongoose.models.file.create({
name: filepath,
content: content
});
}
], (err, model) => {
if (err) {
console.error(err);
}
}); |
Personally, I think async should asyncify promises by default. There are some async functions that I write that I also have to feed into async.queue, but I don't want to write this: import {queue, asyncify} from 'async'
const run = asyncify(async function () {
await someStuff()
})
const q = async.queue(run)
q.push('asdf') where I could be writing this import {queue} from 'async'
async function run () {
await someStuff()
}
const q = async.queue(run)
q.push('asdf') |
Added the docs for |
About this, I was experimenting with use the same codebase and using a Promise interface for methods that use callback as last parameter. check it ! async.waterfall([
function (callback) {
callback(null, 'one', 'two')
},
function (arg1, arg2, callback) {
// arg1 now equals 'one' and arg2 now equals 'two'
callback(null, 'three')
},
function (arg1, callback) {
// arg1 now equals 'three'
callback(null, 'done')
}
]).then(function (value) {
console.log(value === 'done')
}) what do you think about it? I think that could be easy to adapt the library. See for example how works got library handle cb and promise. |
Very interesting. If Async properly handled functions that return promises, then it could also accept promisify-ed Async functions pretty easily. This would work: async.parallel([
async.waterfall([
asyncFn1,
function (result1, next) {
//...
}
asyncFn2,
//...
]), // no callback!
async.each(arr, function (item, cb) {
//...
})
otherAsyncFn
], done) It would be much easier to combine Async functions. The problem today is that callbacks are optional. Leaving off the last argument still executes the task. I would love to make is so Async functions are automatically curried -- if you leave off the callback, it partially applies the function, and all you need to do is call it with a callback. But seeing as many methods have optional parameters, and we're moving towards optional callbacks across the board, you cant really do it. Our methods are variadic, and you cant curry variadic functions. However, if leaving off the final callback off a method has it return a Promise, and if Async is set up to handle functions that return promises, is an interesting combo. One major issue is then Async would have to integrate with a promise library -- which one? Do we rely on |
I feel that Promises is a workflow align with the last node versions (>=4). In this versions Promises are available, so, my vision is use Promise workflow when the global environment have Promises. It's possible add a tiny polyfill (check pinkie-promise) but in my opinion don't have sense provide a polyfill. Better force the user upgrade the node version for use the last node features. Really, check (got 6 PR)[https://github.com/sindresorhus/got/pull/140]. Dependencies are not welcome in very tiny project and are used in everywhere. Maybe this feature could be nice to be included after async modularization, that the codebase will be more easy to adapt. But definetly, align async with promises is a totally must! |
Promises are becoming the first class async functions. So it's hard to imagine the must-heave library for asynchrony without full support of them. I think it could be implemented without polyfill but with feature detection: allow if there is a global Promise object with |
@aearly bluebird adds browser compatibility to the list of it polyfilling. I think it might be appropriate to use it. |
@martinheidegger Bluebird is too big for this. It will cast 76 Kb (minified) for simple support of promise methods. |
Again, use callback or promises interface in your backend workflow is a natural process based in your node version.
But In any case you not have a mixed style. So is not necessary add a dependency for support Promises; If async supports promises one day in the future, you can use it if your node version supports Promises. Extreme case: I'm using a a older node version but my backend is written using promises style. Then you have have defined a Promises object as global or define one before use async. Whatever you want. Simply. Same behavior for the frontend side. Not dependency is necessary. |
Hmmn, it seems the same problems Promises had 4-5 years ago still apply. If everyone was using node 4 or later, and modern browsers with ES6 support, I we could easily utilize promises internally where applicable. The problem is we leave our node 0.12 and older browser users in the dust, requiring them to polyfill. Which polyfill do they use? We wouldn't want to bundle one, even pinkie-promise has weight to it. Also, people who are using promises outside of the standard ES6 Promise will want to use bluebird or q etc., whatever it is that they're using. I don't want to require the use of a polyfill -- Async has also been somewhat of a reaction against Promises -- an alternative way to manage asynchronous code. To start using Promises seems like a step back from that. Properly managed callbacks can be just as powerful as promises (and in many cases, faster, since Promises have a built-in event-loop deferral). I'm all for interop with promises, if a function is passed a Promise or other "thennable" than we should definitely take advantage of it. But I think we should avoid creating and/or returning Promises internally, just due to the fact ES6 support across platforms still has a way to go. |
Agreed, promises aren't cheap to create or process. I'd rather see something like this as an opt in extension. Promises are great but you won't always want them |
IMHO, after migrating a lot of node's code to promises + co + yield, i did not found direct replacement only for |
I like @Kikobeats' little extension, it may make sense to recommend it in the readme (/cc @aearly). I would be strongly against official support of promises in async for reasons brought up by myself and other users. |
@megawac I Added support for callback style,tests and browser build in promise-async. Then if anybody want to use Promise, I think that is ready 👍 |
@aearly, that'd be fine I suppose (so =) but I'd rather leave it in plugin On Fri, Jan 22, 2016 at 2:17 PM, Kiko Beats notifications@github.com
|
Here's another package that promisifies all the available async methods: |
I think just like there is async.asyncify there could be a async.promisify function. Then I can just await async.promisify(async.mapLimit(x,10, mapper)) |
I wouldn't object to that if you want to create a pull request
…On Sat, Dec 17, 2016 at 4:57 PM, Manoj Patel ***@***.***> wrote:
I think just like there is async.asyncify there could be a async.promisify
function.
Then I can just await async.promisify(async.mapLimit(x,10, mapper))
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#956 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/ADUIEKJIDulPHAn_SeEZbiPb3t7ORGnpks5rJFqvgaJpZM4Gh1fr>
.
|
I'm using
|
Add promise support to allow mix different tasks:
The text was updated successfully, but these errors were encountered: