了解JavaScript中的异步/等待模式(Understanding Async/Await patterns in JavaScript)
我对JavaScript编程比较陌生,所以回调函数一直给我带来麻烦。 我使用的是一个支持ES7(Meteor)的“async / await”语法的框架,但是我很难理解如何实现一个调用其他函数的异步函数。
我想要做的事:用户输入一个URL,从URL中提取数据,处理并放入数据库。 我希望这是以非阻塞的方式进行的,以便用户可以在发生此过程时继续使用该应用程序。
伪代码:
async function foo(URL) { try { //using request-promise npm package to make the request const data = await makeRequestPromise(URL) const parsedData = await myParsingFunciton(data); MyDB.insert({ parsedData, }); catch (err) { console.log(err); }
所以我有几个问题:
- 在这个异步函数的非阻塞范围内发生了什么?
- 如果是这样,我可以只使用一系列同步函数,因为在我收到数据之前我无法解析数据?
- 如果我尝试等待未定义为异步的函数(例如myParsingFunction),会发生什么情况? 我的研究似乎暗示该功能被迫返回一个承诺,但我不确定如何确定。 在代码中这样做并不会返回错误,但JavaScript在回报方面似乎令人惊讶地容忍古怪。
我想补充一点,我的函数可以工作,而且事情进入数据库,但我不知道如何测试它是否实际上是非阻塞的。
I'm relatively new to JavaScript programming, and so callbacks have been giving me trouble. I'm using a framework that supports the "async/await" syntax from ES7 (Meteor), but I'm having some difficulty understanding exactly how I can implement an asynchronous function that calls other functions.
What I want to do: The user enters a URL, data is fetched from the URL, processed, and put into a database. I want this to occur in a non-blocking manner, so that the user can continue to use the application while this process is occurring.
Pseudocode:
async function foo(URL) { try { //using request-promise npm package to make the request const data = await makeRequestPromise(URL) const parsedData = await myParsingFunciton(data); MyDB.insert({ parsedData, }); catch (err) { console.log(err); }
So I have a few questions:
- Is anything that happens in the scope of this async function non-blocking?
- If so, can I just use a series of synchornous functions since I can't parse the data before I receive it anyway?
- What happens if I try to await a function that is not defined as asynchronous (for example myParsingFunction)? My research seems to imply that the function is forced to return a promise, but I'm not certain how I would determine that. Doing so in the code does not return an error, but JavaScript seems surprisingly tolerant of weirdness with regards to returns.
I would like to add that my function works and things go into the database, but I have no idea how to test if it is actually non-blocking.
原文:https://stackoverflow.com/questions/42677043
最满意答案
相关问答
更多-
工作代码! 它输出你想要的所有东西! 更新你的代码我有一个错误,我也修复了.. Ranger2将始终驻留在undefined,因为当计数为数组时没有3个站 ,记住javascript中的数组从0(零)开始计数。 我将Drew barontini改为“0” addRanger(lighthouseRock, "Nick Walsh", "magnification burn", 2); addRanger(lighthouseRock, "Drew Barontini", "uppercut launch", ...
-
你的window.country对象实际上是一个数组。 Chrome的控制台正在向您展示。 正如您所看到的,您的对象包含在[] ,这意味着它位于数组中。 尝试访问它,如: window.country[0].iso 。 Your window.country object is actually an array. Chrome's console is showing you that. As you can see your object is wrapped in [], that means it' ...
-
你有一个错误的类定义 。 你需要this而不是var声明的局部变量。 function Destination() { this.city = ""; this.flightNumber = ""; this.type = ""; } 顺便说一句,我建议使用大写首字母的类声明标准。 You have a wrong class definition. You need this and not local variables with var declaration. functi ...
-
初始化对象时,默认情况下未定义。 这行代码在$watch方法中,我添加了if语句来修复问题。 if ($scope.selectedMonth !== undefined) { console.log($scope.selectedMonth.name); } When object was initialized it was undefined by default. This line of code is inside $watch method and I added if st ...
-
访问给定未定义错误的JavaScript对象属性和函数(Accessing JavaScript object properties and functions giving undefined error)[2024-02-17]
你在diceObjectName中导出你的对象。 所以为了使用它,你必须这样做: var dice = require("./dice").diceObjectName; 例 You are exporting your object inside diceObjectName. So in order to use it you have to do so: var dice = require("./dice").diceObjectName; Example -
parseFromString不会从address.xml文件加载数据。 顾名思义,它只会从字符串中解析XML文档,如下所示: var dom = new DOMParser(); var xmlDoc = dom.parseFromString("test","application/xml"); 您需要一个单独的XHR(Ajax)请求来加载该文件中的数据。 此外,您应该使用console.log而不是alert来调试它。 您将能够真正看到该对象中的内容(在您的情况 ...
-
下面是你要做的事情的一个实例 小提琴: http : //jsfiddle.net/yjTXK/1/ var Obj1 = function (){ this.getResult = function() { var result = 5*5; return result; }; this.answer = this.getResult(); }; var Obj2 = function(obj1) { //assign the answ ...
-
我发现了一个小故障。 像往常一样,这是我的弱点,数学。 在我定义dpixel和color变量的行上,我不需要为索引添加1。 我的数组默认存储了索引为0-224的变量,因此基于0的循环是可以的。 我怀疑最后一个元素给了我错误,但我无法证明它尽管我记录每个元素以检查它是否未定义。 I have found the little glitch. As usual it is in my weakness, math. On the lines where I define the dpixel and color ...
-
在实例化时你无法得到this 。 你可以做: Type= { Container: $get('ctl00_Main_rbtnlst_Type'), Local: function(){return this.Container.getElementsByTagName('input');}, Foreign: function(){return this.Container.getElementsByTagName('input');} } 然后使 ...
-
Javascript对象属性在控制台中可见,但未定义?(Javascript object properties visible in console, but undefined?)[2022-09-10]
函数getProfile调用FB API函数FB.api ,它执行异步HTTP请求。 在你的loadProfile函数调用中,你调用getProfile,它立即返回没有填充数据的facebook.profile对象,因为HTTP请求还没有完成。 考虑以下变化: getProfile : function(fCallback) { var bInfo = false, bPhotos = false; FB.api('/me', function(response) { ...