-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
Async/await guide #7506
Comments
Also, the example could be updated to:
|
…en if it's not a fully-fledged promise) See also Automattic/mongoose#7506
…en if it's not a fully-fledged promise) See also Automattic/mongoose#7506
Please check DefinitelyTyped/DefinitelyTyped#32874 In this sample, for DefinitelyTyped/DefinitelyTyped@4b0c6cd , I have marked as promise only when But for the others, maybe DocumentQuery could inherit from PromiseConstructor ? : see https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/mongoose/index.d.ts#L1715 As it we could use |
There is some nuance that prevents us from having queries inherit from promises. See https://mongoosejs.com/docs/queries.html#queries-are-not-promises for more details. With #7398 we should be able to make queries extend from promises. I'm not sure I like adding parentheses after queries return value. Maybe we add examples of using async/await with queries to the API docs instead? |
Ok I see, But for DefinitelyTyped/DefinitelyTyped#32874 the problem is that if Query doesn't inherit from If you say that it's not a good idea to say in the TypeScript definition file that Query inherits from Promises, maybe in the doc we could add a small note to say that tslint errors |
Hi @JulioJu, thanks for your concern for my issue. As we know, 'Queries' are not Promises but 'thenable' objects. //This will return a thenable object.
function foo(x) {
return {
then: function(resolve, reject) {
setTimeout(resolve, 3000, 'The x is: ' + x);
return this;
}
};
}
(async () => await foo(2))().then(console.log);//Use await to execute the thenable object. I guess this is how Queries support await command. So, what if this is a issue of tslint? Checking whether the object has a then method, instead of checking whether the object is a Promise. I don't use TypeScript and tslint. So there may be something I don't know. I'll be happy if you point them out. |
@libook tanks. Maybe a better example is the following (Query is in Mongoose library, myMongooseService could be in a Service file, and the last line in a route file provider, for instance). function Query(x) {
return {
then: function(resolve, reject) {
setTimeout(resolve, 3000, 'The x is: ' + x);
return this;
}
};
}
myMongooseService = async () => {
const x = await Query(2);
console.log('coucou', x);
return x;
}
myMongooseService().then(m => { console.log('main', m); }); Preceding could be polyfill in myMongooseService = () => {
return Promise.resolve(Query(2))
.then(r => {
console.log('coucou', r);
});
};
myMongooseService().then(m => { console.log('main', m); }); Preceding could be polyfill in myMongooseService = () => {
return Query(2)
.then(r => {
console.log('coucou', r);
});
};
myMongooseService().then(m => { console.log('main', m); }); Note: your sample could be polyfill in (() => {
return Promise.resolve(foo(2));
})().then(console.log); or simply Promise.resolve(foo(2)).then(console.log); or more simply (this sample show that foo(2).then(console.log); The preceding samples work, because each |
With further tests, |
…en if it's not a fully-fledged promise) See also Automattic/mongoose#7506
Therefore, for DefinitelyTyped/DefinitelyTyped#32874 all it's ok, I've tested ;-). For the doc https://mongoosejs.com/docs/api.html I propose to comment like following:
Maybe somewhere in the doc, something like #7506 (comment) could be added to explain how and why Mongoose work with |
@JulioJu adding an async/await guide is a good idea. There's a lot of nuance with using async/await, that's why I wrote a book about it. We'll also add a note that queries are thenable to the API docs |
I'd be happy to write this if it's still an issue of interest. Would you like me to fork and create a new .pug file, or edit it into an existing file? |
@Rossh87 sure, new |
@vkarpov15 I've got a PR ready to go that includes the new |
Feel free to submit what you have. We generally don't commit newly generated docs from running |
Hi @libook @mhombach @vkarpov15
Following the issue #7461 I would propose to improve API documentation.
For instance, for https://mongoosejs.com/docs/api.html#model_Model.findOneAndUpdate
[Note: I've deleted a proposal here, because it was so bad]
Or instead as
A query is not a fully-fledged promise
(see https://mongoosejs.com/docs/promises.html) we could say instead:Like that a user that know what is a Promise and a thenable and what is a callback could know easy how to use this function. When I've started with Mongoose, I thought falsely « dash, mongoose API doesn't return Promise contrary to the mongodb native driver ».
It should be done for all CRUD operations, into lib/model.js and lib/query.js.
Furthermore https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/mongoose/index.d.ts should be updated accordingly. At least for this TypeScript definition file, we could say that: « findOneAndUpdate and others CRUD operations are Promises ».
What do you think about that?
The text was updated successfully, but these errors were encountered: