Skip to content
Advertisement

Tag: asynchronous

Async function without await in JavaScript

I have two functions, a and b, that are asynchronous, the former without await and the latter with await. They both log something to the console and return undefined. After calling either of the function, I log another message and look if the message is written before or after executing the body of the function. If you launch test without

Using await within a Promise

There seems something inherently wrong with having to define a Promise’s callback as asynchronous: This is apparently an antipattern and there are coding problems which can arise from it. I understand that it becomes easier to fail to catch errors here, even when placing await statements inside try/catch blocks. My first question is, what’s the best way to code something

When to mark function as async

Basically, function must be prefixed with async keyword if await used inside it. But if some function just returns Promise and doesn’t awaiting for anything, should I mark the function as async? Seems like both correct or not? Answer if some function just returns Promise and doesn’t awaiting for anything, should I mark the function as async? I would say

Is it an anti-pattern to use async/await inside of a new Promise() constructor?

I’m using the async.eachLimit function to control the maximum number of operations at a time. As you can see, I can’t declare the myFunction function as async because I don’t have access to the value inside the second callback of the eachLimit function. Answer You’re effectively using promises inside the promise constructor executor function, so this the Promise constructor anti-pattern.

Why does .json() return a promise?

I’ve been messing around with the fetch() api recently, and noticed something which was a bit quirky. post.data returns a Promise object. http://jsbin.com/wofulo/2/edit?js,output However if it is written as: post here is a standard Object which you can access the title attribute. http://jsbin.com/wofulo/edit?js,output So my question is: why does response.json return a promise in an object literal, but return the

Why couldn’t popular JavaScript runtimes handle synchronous-looking asynchronous script?

As cowboy says down in the comments here, we all want to “write [non-blocking JavaScript] asynchronous code in a style similar to this: ” So people have come up solutions to this problem like callback libraries (eg async) promises event patterns streamline domains and generators. But none of these lead to code as simple and easy to understand as the

Advertisement