异步JavaScript中存在哪些保证? [英] What guarantees exist in asynchronous JavaScript?

查看:132
本文介绍了异步JavaScript中存在哪些保证?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通常对异步代码(尤其是JavaScript)是陌生的,并且一直被

I am new to asynchronous code in general (and JavaScript in particular) and have been falling prey to common pitfalls, so I want to re-evaluate my most basic assumptions.

测试下面的代码并没有给我答案,因为仅仅通过测试并不意味着代码将来会以相同的方式运行.

Testing the code below does not give me an answer since, just because the test passed, doesn't mean the code will run the same way in the future.

这个问题很笼统,因此我将给出一些我想了解的基础知识的具体示例.

The question is very general, so I will give a couple concrete examples of the basics I am trying to understand.

const secret_key = "BLAH BLAH";
var url = `https://www.google.com/recaptcha/api/siteverify?secret=${secret_key}&response=${token}`;

如果上述代码出现在异步函数中,是否可以确定url定义正确(即第二条语句将出现在第一条语句之后)?如果secret_key是 var ,这会改变吗?还是 let ?

If the above code occurs in an async function, can I be sure that url will be defined correctly (i.e. that the second statement will occur after the first)? Does this change if secret_key is a var? Or let?

blah = async function() {
    syncFunction();
}

我可以确定 synchronous 函数将在该函数返回之前运行完成(即使它需要做一些耗时的工作)吗?

Can I be certain that the synchronous function will be run to completion before the function returns (even if it has to do some time-intensive work)?

我问是因为我的理解是,这对于异步函数(

I ask because my understanding is that this would not be true for an asynchronous function (as explained here) unless I use await / promises.

最后,此堆栈溢出问题解决了我的问题.基本上我使用的是await,但是调用的函数有一个未定义的Promise.

In the end, this Stack Overflow question resolved my problem. Basically I was using await, but the function being called had an undefined Promise.

推荐答案

第一个示例

我可以确定url的定义是否正确(即第二个语句将在第一个之后出现)?

can I be sure that url will be defined correctly (i.e. that the second statement will occur after the first)?

是的,绝对如此.这些只是两行常规代码,您只需将两个字符串存储在两个变量中即可;当然它们将按顺序执行,这里没有异步.

Why yes, absolutely. These are just two regular lines of codes, you simply store two strings in two variables; of course they will be executed in order, there's nothing asynchronous here.

第二个示例

使用Typescript代替Javascript有助于我理解 async 函数的工作原理.这是您在Typescript中的示例:

Using Typescript instead of Javascript helped me a lot to understand how async functions work. Here's your example in Typescript :

const syncFunction = ():string => {
    return "hello"
}

const blah = async ():Promise<string> => {
    return syncFunction()
}

(async () => {
    const sayHello:string = await blah();
    console.log(sayHello) // "hello"
})()

如您所见,添加类型会有所帮助. async 函数始终返回一个Promise.如果返回true ,则返回的类型不是 boolean ,而是 Promise< boolean> .

As you can see, adding the types helps. An async function always returns a Promise. If you return true, then the returned type isn't boolean, it is Promise<boolean>.

因此, return syncFunction()返回"hello"的承诺.因此,您必须等待它才能真正得到您的"hello"消息.细绳.然后您可以记录它.

So, return syncFunction() returns a Promise of "hello". Therefore, you must await it to actually get your "hello" string. Then you can log it.

相同的代码已转译为JS,因此可以在代码段中运行:

Same code transpiled to JS so it is runnable in a snippet :

const syncFunction = () => {
    return "hello"
}

const blah = async () => {
    return syncFunction()
}

(async () => {
   const sayHello = await blah();
    console.log(sayHello) // "hello"
})()

这篇关于异步JavaScript中存在哪些保证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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