How not to forget that you use everywhere in Javascript?

Trying to write a small chrome extension that relies on callback request functions in the chrome.* Interface, I quickly landed on promises and async / await, since I needed to guarantee the order of certain operations, trying to avoid the callback hell .

However, as soon as I introduced async / await into some functions, each function that used them also had to be turned into an async function in order for await to be able to return a value. In the end, even some global constants became promises, for example.

 const DEBUG = new Promise(function(resolve){ chrome.management.getSelf(resolve); }).then(function(self){ return self.installType == 'development'; }); 

However, now I need to write await everywhere, and the appearance of strange errors, such as if(DEBUG){...} , is always done too simply.

While it seems that you can identify errors with ESLINT , the await entry everywhere seems unnecessarily cumbersome, and so I was wondering if , if Javascript has some better construction, what am I missing?

(Subjectively, my current use of await / async seems to be reversed, promises are stored as is, unless explicitly expected, but it seems to me more desirable that promises wait by default in async functions and only be saved as naked promises when explicitly requested.)

+10
javascript asynchronous async-await
source share
1 answer

Due to the lack of a type system that would make it easy to catch such errors (have you considered Typescript or Flow?), You can use the System Hungarian notation for variable names. Select a suffix prefix, such as P , Promise or $ and add it to all promise variables, similar to how asynchronous functions are often referred to as Async suffix. Then only do things like

 const debug = await debugPromise 

where you can quickly see that if (debug) is good, and if (debugPromise) is not.


After I introduced async / await into some functions, each function that used them also had to be turned into an asynchronous function so that a return value could be expected. In the end, even some global constants became promises.

I would not do that. Try to make as few functions asynchronous as possible. If they themselves do not perform essentially asynchronous operations, but rely only on the results of some promises, declare these results as parameters of the function. A simple example:

 // Bad async function fetchAndParse(options) { const response = await fetch(options); // do something return result; } // usage: await fetchAndParse(options) 

 // Good: function parse(response) { // do something return result; } // usage: await fetch(options).then(parse) // or parse(await fetch(options)) 

The same template can be applied to global variables - either make them explicit parameters of each function, or make them parameters of a function of a module that contains all the others as closures. Then await global promises only once in a module before declaring or executing anything else, and subsequently using a simple result value.

 // Bad: async function log(line) { if (await debugPromise) console.log(line); } async function parse(response) { await log("parsing") // do something return result; } … await parse(…) … 

 // Good: (async function mymodule() { const debug = await debugPromise; function log(line) { if (debug) console.log(line); } function parse(response) { log("parsing") // do something return result; } … parse(…) … }()); 
+5
source share

All Articles