async-await

Are you there, asynchronously written value?

守給你的承諾、 提交于 2020-07-03 06:26:08
问题 The last couple of days I've been reading about async/await. Yesterday I found this video on Channel 9 what made wonder about some things. Please consider the slide below. Aside from the issues Lucian Wischik addresses, I wondered about variable assignment. Say we changed the async void into async Task , and added await before the SendData call. This enables us to get the stream, assign the variable m_GetResponse , wait for two seconds and print it. But what happens to the variable? It could

Are you there, asynchronously written value?

时光毁灭记忆、已成空白 提交于 2020-07-03 06:25:33
问题 The last couple of days I've been reading about async/await. Yesterday I found this video on Channel 9 what made wonder about some things. Please consider the slide below. Aside from the issues Lucian Wischik addresses, I wondered about variable assignment. Say we changed the async void into async Task , and added await before the SendData call. This enables us to get the stream, assign the variable m_GetResponse , wait for two seconds and print it. But what happens to the variable? It could

How to get returned value without await opeartor

吃可爱长大的小学妹 提交于 2020-07-02 09:47:14
问题 I need to get the returned value without await operator(in below sample I need to get the "hello world" in var y without await operator). Because one method is referred to a lot of places.But my requirement is that method is executed at asynchronously for particular operations. None other time it is enough to be executed as synchronously. If I put await in all referred places that method needs to change as async and also referred methods need to change as async and await. Is there any

Testing async HTTP-based function with Jasmine and await: Expected one matching request for criteria “…”, found none

一个人想着一个人 提交于 2020-06-29 05:20:54
问题 WHAT: Testing an async function that uses await WITH: Angular 9, Jasmine 3.4.0 Minimal code to reproduce: StackBlitz I have a function like this. Note, that it has to await this.getHeaders(). Whenever I remove await and replace the implementation of getHeaders() by some synchonous implementation, the test runs successfully. What is the correct way to test it? private async loadUserData() { // Try to remove await - the test runs successfully //const headers = this.getHeaders(); const headers =

Xamarin.Forms Page.DisplayAlert in Page.OnAppearing

余生长醉 提交于 2020-06-29 04:12:11
问题 I am unable to get a DisplayAlert popup to show within the OnAppearing callback of a Xamarin.Forms Page . Here is what I have tried so far: protected override void OnAppearing() { base.OnAppearing(); this.DisplayAlert("Alert", "Consider yourself alerted", "OK"); } Considering DisplayAlert is technically an async function, returning a Task , I have also tried the following: protected async override void OnAppearing() { base.OnAppearing(); await this.DisplayAlert("Alert", "Consider yourself

Will an instance be garbage collected if a Task's callback get reference to the instance itself?

社会主义新天地 提交于 2020-06-29 04:08:30
问题 public class Cls { public object Obj { get; set; } public async void Func() { Task.Run(() => { Thread.Sleep(999999999); this.Obj = new { }; }); } } public class Program { public static void Main(string[] args) { new Cls().Func(); } } Please consider the above codes and neglect if it makes sense first. In the above case, I did not store instance of Cls into any variable, seems that nothing is referencing that instance and it would be GC. However, there is a Task.Run() in side Func . The

file.slice fails second time

喜夏-厌秋 提交于 2020-06-29 03:59:06
问题 I'm trying to make a (front-end) Javascript that would be able to copy very large files (i.e. read them from a file input element and 'download' them using StreamSaver.js). This is the actual code: <html> <header> <title>File copying</title> </header> <body> <script src="https://cdn.jsdelivr.net/npm/web-streams-polyfill@2.0.2/dist/ponyfill.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/streamsaver@2.0.3/StreamSaver.min.js"></script> <script type="text/javascript"> const

file.slice fails second time

烂漫一生 提交于 2020-06-29 03:58:14
问题 I'm trying to make a (front-end) Javascript that would be able to copy very large files (i.e. read them from a file input element and 'download' them using StreamSaver.js). This is the actual code: <html> <header> <title>File copying</title> </header> <body> <script src="https://cdn.jsdelivr.net/npm/web-streams-polyfill@2.0.2/dist/ponyfill.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/streamsaver@2.0.3/StreamSaver.min.js"></script> <script type="text/javascript"> const

Getting Query object instead of results with Async/await function and npm-mysql

有些话、适合烂在心里 提交于 2020-06-29 03:49:12
问题 I have this function which is async and i'm trying to make a simple query from npm-mysql db. let sortCategory = async (id) => { try { var sql = 'SELECT * FROM categories WHERE parent_id=?'; var results = await connection.query(sql, id); // console.log(results); return(results); } catch(err) { console.log(err); return false; } } But instead of results inside the results variable i just get the query object. Query { _events: [Object: null prototype] { error: [Function], packet: [Function],

Unexpected `await` inside a loop. (no-await-in-loop)

杀马特。学长 韩版系。学妹 提交于 2020-06-29 03:47:20
问题 How Should I await for bot.sendMessage() inside of loop? Maybe I Need await Promise.all But I Don't Know How Should I add to bot.sendMessage() Code: const promise = query.exec(); promise.then(async (doc) => { let count = 0; for (const val of Object.values(doc)) { ++count; await bot.sendMessage(msg.chat.id, `💬 ${count} and ${val.text}`, opts); } }).catch((err) => { if (err) { console.log(err); } }); Error: [eslint] Unexpected `await` inside a loop. (no-await-in-loop) 回答1: If you need to send