简体   繁体   English

除非明确地覆盖了“异步”功能,它会立即返回resolve(undefined)吗?

[英]Does an 'async' function immediately return resolve(undefined) unless this is explicitly overrided?

By overriding an async function's immediate return of resolve(undefined), I mean using: 通过覆盖异步函数的resolve(未定义)立即返回,我的意思是使用:

return new Promise(resolve, reject)... resolve(variableName)

Also, if this is indeed the default behaviour, are there any other ways to override this? 另外,如果这确实是默认行为,是否还有其他方法可以替代此行为?

Edit: New understanding thanks to @Quentin. 编辑:感谢@Quentin的新理解。 Please correct the following if wrong. 如果错误,请更正以下内容。

When you call an async function like async function1() {... without await: 当您调用async function1() {...这样的async function1() {...而没有等待时:

function1 will block (only 'asyncing' tasks with await or then) and will run to completion. function1将阻塞(仅在等待或之后“异步”执行任务)并将运行完成。

function1 does not immediately return anything (ie an empty, undefined, or pending promise). function1不会立即返回任何内容(即,空,未定义或待处理的承诺)。

function1 returns Promise {undefined} if there's no explicit return statement. 如果没有显式的return语句,则function1返回Promise {undefined}。

function1 returns Promise { pending } if there's await or return new Promise.. , as these add a task to the event loop microtask queue. 如果有await则function1返回Promise {待处理}或return new Promise.. ,因为它们将任务添加到事件循环微任务队列中。

And function1 fulfils/rejects the promise with a value if return varName is used. 如果使用return varName则function1将用一个值实现/拒绝promise。

An async function will run and return a promise. 一个async函数将运行并返回一个Promise。

When the function gets to the end, it will resolve that promise with what would normally be the return value. 当函数结束时,它将使用通常为返回值的方式解决该承诺。 (If you use await inside, then the promise won't be resolved until the async function unfreezes and reaches the end.) (如果await内部使用await ,那么直到async函数解冻并到达末尾时,promise才会被解决。)

The default return value for a function without a return statement is undefined , so if you don't include a return statement it will return a promise that resolves with undefined . 没有return语句的函数的默认返回值是undefined ,因此,如果不包括return语句,它将返回一个以undefined解析的promise。

If you return a promise, then it will return a promise resolved with that promise which will be adopted as normal. 如果您返回一个承诺,那么它将返回一个已解决的承诺,该承诺将被正常采用。

If you return something else, then the promise will be resolved with that value. 如果您返回其他内容,则将使用该值来兑现承诺。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM