获取JavaScript承诺值 [英] Getting JavaScript promise value

查看:63
本文介绍了获取JavaScript承诺值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个json文件,我正在尝试读取其中的内容以供在脚本中使用.我正在使用以下内容来获取json:

I have a json file I am trying to read the contents of for use in a script. I am using the following to fetch json:

const json = fetch('Data/my_data.json').then(response => response.json());

当我这样做时,我在控制台中查看json对象,我看到它是一个包含了来自文件的实际json对象的返回的Promise(已解决状态).但是,您不能直接访问promisevalue.您必须再次使用then语句,所以我使用了它:

When I do this and I look at the json object in the console I see it is a returned promise (resolved status) containing the actual json object from the file. You can't directly access the promisevalue though. you have to use a then statement again so I used this:

json.then(function(getData){metaData = getData;});

未将metaData对象设置为原始Promise的值.直到第二次完成时才设置它.该信息已在最初返回的诺言中提供.

The metaData object is not set to the value of the original promise. It is unset until a later time when the 2nd then completes. The information is already available though in the originally returned promise.

是否可以在没有第二个then语句的情况下获得原始的promisevalue或 我使用第二个then语句错误地访问了诺言.

Is it possible to get the original promisevalue without the 2nd then statement or am I accessing the promise incorrectly with the 2nd then statement.

感谢您的见解!

推荐答案

是否可以在没有第2条then语句的情况下获得原始的promise值,或者我是否有第2条then语句错误地访问了promise.

Is it possible to get the original promisevalue without the 2nd then statement or am I accessing the promise incorrectly with the 2nd then statement.

您必须使用.then()await来使数据超出承诺.数据是异步检索的,因此,只有等待.then()await才能使它脱离承诺.而且,如果使用await,则必须在async函数内部.

You must use either .then() or await to get the data out of the promise. The data is retrieved asynchronously so the only wait to get it out of the promise is with .then() or await. And, if using await, it would have to be inside an async function.

promise和async/await对于管理异步操作很有帮助,但是它们不能更改以下事实:数据是异步检索的,并且将无法同步使用.因此,您必须使用异步工具来获取数据.在这种情况下,它是.then()await.

While promises and async/await are helpful for managing asynchronous operations, they cannot change the fact that the data is retrieved asynchronously and will not be available synchronously. So, you have to use asynchronous tools to get to the data. In this case, that's .then() or await.

并且,要使用await,您必须处于支持它的环境中.现代浏览器确实支持它(并且您已经在使用fetch(),这需要一个半现代浏览器),但是某些浏览器仍未使用某些较旧的浏览器,因此需要考虑.在这种情况下,您可能应该使用.then().

And, to use await, you have to be in an environment that supports it. Modern browsers do support it (and you're already using fetch() which requires a semi-modern browser), but not some of the older browsers still in use by some so that is a consideration. In this case, you should probably use .then().

还要注意,在此编码结构中:

Also, note that in this coding structure:

json.then(function(getData){metaData = getData;});

您要将承诺中的数据分配给更高范围的变量,几乎总是一个警告信号,表示您正在尝试在未知范围内使用数据何时实际可用数据和做错事情的时间.

where you're assigning the data from the promise to a higher scoped variable is nearly always a warning sign that you're attempting to use the data in a scope in which you don't know the timing for when the data is actually available and doing something wrong.

可以安全使用数据的唯一作用域是 INSIDE (.then()处理程序)或在其中调用并将数据传递到的函数中.不要将其分配给更高的范围,然后尝试在更高的范围中盲目使用.您不会知道数据何时真正有效.您只知道.then()处理程序中的计时信息.

The ONLY scope that you can safely use the data is INSIDE the .then() handler or in a function that you call from there and pass the data to. Don't assign it to a higher scope and then try to just blindly use in that higher scope. You won't have any idea when the data is actually valid. You only know that timing information inside the .then() handler.

json.then(function(data){
    // use the data here
    // write the code in here that uses the data
    // or call some function here that you pass the data to
    processData(data);
});
// can't use the data here (it's not yet available)

有关此问题的进一步说明,请参见:

For further explanation of this issue see: How do I return the response from an asynchronous call?

这篇关于获取JavaScript承诺值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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