然后没有承诺的Java脚本返回val [英] Javascript then without promise return val

查看:39
本文介绍了然后没有承诺的Java脚本返回val的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

也许我在Google上搜索不正确.然后没有参数的函数不会阻塞?例如,您有一个承诺:

Perhaps i'm not googleing correctly. does a then function without a paramater not block? for instance, you have a promise:

someFunc = () => {
  return new Promise((res,rej)=>{
   somethingAsync(input).then((val) => res(val))
  })
}

在我们的功能的以下实现中.都会等待someFunc返回val吗?

in the following implements of our function. would both wait for the someFunc return val?

someFunc().then(dosomethingafter())
someFunc().then((val) => dosomethingafter())

推荐答案

在JS中,急切需要对表达式进行求值.这意味着每个函数参数在传递前都要在之前求值.

In JS expressions are eagerly evaluated. It means every function argument is evaluated before it's passed.

someFunc().then(dosomethingafter())

实际上与

var tmp = dosomethingafter();
someFunc().then(tmp)

因此在调用 then 之前先调用 someFunc().then(dosomethingafter())函数,并将其返回结果作为参数传递.

so a function someFunc().then(dosomethingafter()) is invoked before then is called, and its returned result is passed as a parameter.

您可能要说的是

someFunc().then(dosomethingafter)

请注意,没有函数调用-仅将对函数的引用传递给然后,然后在解决承诺后将调用该函数.

note there is no function call - only a reference to a function is passed to then and it would then be called when a promise is resolved.

这篇关于然后没有承诺的Java脚本返回val的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆