在等待还是没有返回之间有区别
我只是想着我们需要使用await操作符,以下两种情况有什么区别。
一
public async updateOne(Model, doc, errorMsg?){ return await Model.findOneAndUpdate({'_id': doc._id}, {'$set': doc}, {upsert:true, new: true}).lean().exec(); }
二
public updateOne(Model, doc, errorMsg?){ return Model.findOneAndUpdate({'_id': doc._id}, {'$set': doc}, {upsert:true, new: true}).lean().exec(); }
我认为结果没有什么区别,但是我认为完全没有必要使用async await,因为promise会被返回,我们只需要在asynchronous函数中调用updateOne函数时使用await操作符。
正如Oyverus所回答的 ,结果没有任何区别,但是使用async
函数会创build另一个Promise,我已经为每个案例添加了一些粗略的示例。
使用async updateOne(){ return await Model.findOneAndUpdate(...) }
结果在Promise代码如下所示:
return new Promise((resolve, reject) => { Model.findOneAndUpdate(...).then(resolve, reject) })
然后async updateOne(){ return Model.findOneAndUpdate(...) }
是:
return new Promise(resolve => resolve(Model.findOneAndUpdate(...))
一个普通的updateOne(){ return Model.findOneAndUpdate(...) }
是:
return Model.findOneAndUpdate(...)
我倾向于在可能的情况下使用简单返回,但在代码中留下/*async*/
以用于文档目的。
/*async*/ write(){ return this.db.write() }
在这个用例中,最终的结果没有什么区别,但是它的工作方式有所不同。
在一个asynchronous函数中返回一个期待的承诺将返回另一个承诺,这个承诺被等待的承诺的值解决,这基本上是两个承诺,在这个用例中是多余的,因为你没有对承诺做任何额外的处理。
在正则函数中返回一个promise只会返回这个promise,而不会把它封装在另一个promise中,所以你完全正确,不需要用这种方式来使用async。
返回与等待还是没有有区别? …我认为结果没有什么区别
是的,你说得对。 在你的例子中没有区别。
来自MDN:
当一个asynchronous函数被调用时,它返回一个Promise。 当asynchronous函数返回一个值时,Promise将用返回的值parsing。 当asynchronous函数抛出exception或某个值时,Promise将被抛出的值拒绝。
asynchronous函数可以包含一个awaitexpression式,暂停asynchronous函数的执行并等待传递的Promise的parsing,然后恢复asynchronous函数的执行并返回parsing的值。
当我们在asynchronous函数中调用updateOne函数时,我们只需要使用await运算符。
不完全是这样的, async
运算符只是在相应的函数中启用await
,所有的await
运算符在那个asynchronous函数中解开了对在.then(value => /* do stuff with value */)
返回的值的承诺.then(value => /* do stuff with value */)
callback。 目的是这样的代码类似于一个更熟悉的同步控制stream程。
任何async
函数调用的结果(即它的返回types)将是你返回的任何一个Promise,但是请注意,你仍然可以在async
函数中使用unwrapped promise(即不await
),这些值将被包装在promise中。
让我知道,如果你仍然需要帮助理解。 我会修改这个post。
function simplePromise() { return Promise.resolve('resolved value'); } async function asyncAwaitTest() { const simplePromiseValue = await simplePromise(); console.log(simplePromiseValue); // resolved value // if you don't use the `await` keyword, then the promise will still be a promise simplePromise().then(value => console.log(value)); } asyncAwaitTest();
唯一不同的是内存使用情况,因为可能会创build一个中间的Promise对象,所以return await
使用更多。
Async / Await的另一个好处是,它允许我们捕捉一个好的旧的try / catch块的任何意外的错误。 我们只需要像这样包装我们的等待电话:
async function doSomethingAsync(){ try { // This async call may fail. let result = await someAsyncCall(); } catch(error) { // If it does we will catch the error here. } }
catch子句将处理由等待的asynchronous调用或任何其他失败的代码引发的错误,这些代码可能是我们在try块中写入的。
当唯一的返回值是应用一个await
操作符的结果时,你是正确的,你不需要使用async
函数。 从其他地方不使用await
的同步函数返回await
的promise操作数是等价的(正如你在例子“two”中编码的那样)。
逻辑链
当我们在asynchronous函数中调用updateOne函数时,我们只需要使用await运算符
但是并不确切。 您可以在不使用await
情况下从asynchronous函数中返回promise
三
public async updateOne(Model, doc, errorMsg?){ return Model.findOneAndUpdate({'_id': doc._id}, {'$set': doc}, {upsert:true, new: true}).lean().exec(); }
Model
返回的promise将被用于parsingasync
返回的promise。
- Visual Studio不能将JavaScript输出组合成一个文件。 我需要做更多的调整吗?
- 如何在@types上为范围/命名空间包安装TypeScript声明?
- Node.js process.stdin问题typescript(tty.ReadStream vs ReadableStream)
- 在编写自定义TypeScript定义文件时,发生错误“模块名称”在…下parsing为无types模块
- “angular-bootstrap-md \ index.ts不是编译输出的一部分”
- 找不到'core-js'的types定义文件
- Aurelia应用程序“Hello World”根本不工作
- 在Typescript前端和NodeJs后端之间共享js对象的最佳方法
- Angular 2快速入门教程问题(嵌套组件)