How to insert await to async method with quick fix (using ReSharper) - resharper

In some legacy versions of ReSharper it was possible to alt + enter to insert await to invocation of async method. How to enable this feature in current versions?
Example:
dog.WoofAsync(); // alt+enter to insert await

Related

Detect Key Press in System

I am trying to detect a keypress event outside the console tab, I have allready tried input-event (for linux), hotkeys-js (for browser), and iohook (gives error). How can I do it?
I found a way. I used iohook, but the version 0.6.6 since the new version crashed, and called the start method.
iohook.on('keypress', (res) => {
let key = String.fromCharCode(res.rawcode);
console.log(key);
});
iohook.start();

Calling a function that returns a AsyncIterableIterator without using "for await" block

I'm writing an AWS Lambda function in TypeScript using the Node.js runtime. I'm using a "batchDelete" function from a DynamoDB ORM library which returns an AsyncIterableIterator type.
According to the documentation here https://github.com/awslabs/dynamodb-data-mapper-js#batchDelete, I should invoke the method with a for await loop like this:
for await (const found of mapper.batchDelete(toRemove)) {
// items will be yielded as they are successfully removed
}
This all works great but the problem comes in where if I enable ESLint on my project. The default rules throw an error because the for await block is empty. I also get a warning because the found constant is never used. I have no use for the found constant and don't want to log it. I was wondering if there was another way to call an AsyncIterableIterator function where we disregard what is returned and don't have the empty block?
If you don't care about the results of the iteration, then you should probably just do something like this:
await Promise.all(toRemove.map(item => mapper.delete(item));
To use the mapper.batchDelete(toRemove) result more directly, you have to allow for multiple levels of promises. Perhaps you could do this:
await Promise.all(await mapper.batchDelete(toRemove)[Symbol.asyncIterator]());
In doing this, await mapper.batchDelete(toRemove)[Symbol.asyncIterator](), that would get you the default async Iterator and then passing it to Promise.all() would iterate it to get an iterable of promises. Unfortunately, in building it to make this easier:
for await (const found of mapper.batchDelete(toRemove))
they made it a bit more difficult to just get an array of promises out of it.
FYI, here's a link to the code for the .batchDelete() method if you want to look at how it's implemented.

Synchronous mode of Webdriver.io have problems with Promise

I write tests on CoffeeScript using Webdriver.io framework (Wdio testrunner) with sync mode enabled. According to the documentation, Webdriver.io commands should be executed in synchronous mode. However, an unexpected problem appears in the process of using the Promise.
We are considering the simplest test that finds an element on a page by selector and displays the text of the found element to the console.
Example 1 – code without promise
browser.url('... URL ...')
a = browser.$('... selector ...').getText()
console.log(a)
In this example commands of the Webdriver.io work correctly.
Example 2 - the code is in the constructor of the Promise
p = new Promise((resolve, reject) ->
browser.url('... URL ...')
a = browser.$('... selector ...').getText()
console.log(a)
resolve()
)
return p
If the commands are contained in the constructor of the Promise, then they are correctly executed too.
Example 3 - the code is in the block .then after returning the Promise
p = new Promise((resolve, reject) ->
resolve()
).then(() ->
browser.url('... URL ...')
a = $('... selector ...').getText()
console.log(a)
)
return p
The next error message shows in display: "$ (...). GetText is not a function" (Example 3). Apparently, the commands of the Webdriver.io begin to work asynchronously. Though I can use await keyword to process those Promises but I want to execute the code equally in the same way (synchronously) regardless of the location of the code (in the Promise or outside it).
Also switching to asynchronous mode occurs when command Await is used.
Example 4 (Example 1 code using the await keyword)
await console.log('123')
browser.url('... URL ...')
a = browser.$('... selector ...').getText()
console.log(a)
In this case for the correctly work of program it will be necessary to redo all the code, taking into account asynchronous processing.
As a solution, I can write all the tests asynchronously, but the code will become more complicated. Can I work with commands of the Webdriver.io synchronously even when using the Promise?
If you want to use Promise in your wdio test script which in sync mode, then you need to use browser.call() of wdio. More details on call: v5 and v4 .
Here you can find the sample code and more details on how a call is used: How to use 3rd party method that takes callback in webdriverio
Thanks,
Naveen

Vscode API: read clipboard text content

I'm currently trying to write an extension for Visual Studio Code, and I can't manage to understand how to read the clipboard content.
The VSCode API specifies this method:
readText ():Thenable<String>
Following what I read about Thenable, I should be able to get the clipboard's text like that:
var clipboard_content = vscode.env.clipboard.readText().then((text)=>text);
But all I manage to get is a Promise { pending } object.
What I would like to get is the clipboard content as a string
Basics mistake.
Because you use promises (async) and want async/await (linear) functionality.
It should be (promises, async code):
vscode.env.clipboard.readText().then((text)=>{
clipboard_content = text;
/* code */
});
or (sequential code)
let clipboard_content = await vscode.env.clipboard.readText();
/* code */
PS.: In JS, you should use camelCase instead of snake_case when naming variables and functions. This is one of the recommendations of JavaScript Standard Style

Can not find line break setting after opening curly brace in resharper

When I create a new javascript constructor I want to do this:
var PersonViewModel = function(){};
Then I press return after the semicolon and get this:
var PersonViewModel = function()
{
};
But what I want is this:
var PersonViewModel = function()
{
// so I can immediately continue writing code here
};
I want that line break after the opening curly brace.
I can not find the settings in the javascript options, anyone knows please?
ReSharper tries very hard not to mess with user typing (at least when it is correct). It can help you not to touch very much, but if you want to type something fully, it tries to allow you to do it. But the feature that you want breaks this principle: when user would type to function statement side-by-side, the second one would be inside the first one instead.
What I suggest instead is just typing ...function(){ and pressing Enter. ReSharper would add } for you automatically, so you can just continue to write function body. You would get syntactically correct code, because ; could be omitted in JavaScript. ReSharper would highlight missing ; anyway and would suggest to add all missing ; in the whole file. Just use this feature from time to time to make your code beautiful.

Resources