async-await

Xamarin Async method usage OnStart()

北慕城南 提交于 2021-02-20 14:50:33
问题 Is it considered a good practice to call an Async method that does some heavy lifting with server connections and data exchange during OnStart() event of the application given that this method does not touch the UI thread? Are all the components of the application properly initialized at the time of this event firing for the Async method to be able to execute? protected override async void OnStart() { sendHttpRequestAsync(); } private async void sendHttpRequestAsync() { await ... } 回答1: Avoid

Why do javascript functions need to have the keyword “async”? Isn't the “await” keyword enough? [closed]

て烟熏妆下的殇ゞ 提交于 2021-02-20 10:25:16
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed 4 years ago . Improve this question For instance, why does the function below need to have "async".. isn't using await specific enough for the compiler to parse the code without ambiguity? # Why do we need async here async function foo() { var user = await getUser(user_id); console.log

Why do javascript functions need to have the keyword “async”? Isn't the “await” keyword enough? [closed]

夙愿已清 提交于 2021-02-20 10:19:19
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed 4 years ago . Improve this question For instance, why does the function below need to have "async".. isn't using await specific enough for the compiler to parse the code without ambiguity? # Why do we need async here async function foo() { var user = await getUser(user_id); console.log

Is done required in async Jest tests?

半世苍凉 提交于 2021-02-20 09:42:07
问题 I'm having an argument with a co-worker about done() in Jest tests. He's orders of magnitude more experienced with JavaScript than I am, and I came in after async / await was generally accepted, plus came from a .NET environment, so I was used to it. I write my tests like this: it("should return 200 OK for POST method", async () => { await request(app).post("SOMEENDPOINT") .attach("file", "file") .expect(200); }); He's more used to promises so will write his tests like this: it("should return

How should I convert a function returning a non-generic Task to ValueTask?

杀马特。学长 韩版系。学妹 提交于 2021-02-20 06:30:52
问题 I'm working on some code which builds a buffer in memory and then empties it into a TextWriter when the buffer fills up. Most of the time, the character will go straight into the buffer (synchronously) but occasionally (once every 4kb) I need to call TextWriter.WriteAsync . In the System.Threading.Tasks.Extensions package there only appears to be a ValueTask<T> struct, and no non-generic ValueTask (without a type parameter). Why is there no ValueTask , and what should I do if I need to

How should I convert a function returning a non-generic Task to ValueTask?

社会主义新天地 提交于 2021-02-20 06:28:50
问题 I'm working on some code which builds a buffer in memory and then empties it into a TextWriter when the buffer fills up. Most of the time, the character will go straight into the buffer (synchronously) but occasionally (once every 4kb) I need to call TextWriter.WriteAsync . In the System.Threading.Tasks.Extensions package there only appears to be a ValueTask<T> struct, and no non-generic ValueTask (without a type parameter). Why is there no ValueTask , and what should I do if I need to

await for jQuery.when ajax requests

妖精的绣舞 提交于 2021-02-19 09:41:23
问题 This code inside async function, does not give the expected result: var result = await $.when( $.get('/api/1'), $.get('/api/2') ); with one request, result will be the output I expect (the response text). However, with these two requests, the returned result is an array which does not hold the two Promises values. Is there any workaround? I know there are then() and done() , but I prefer using await. 回答1: jQuery's .when() and the native await have different semantics. Compare: // jQuery $

dart:io sync vs async file operations

痞子三分冷 提交于 2021-02-19 09:04:46
问题 There are a number of sync and async operations for files in dart:io: file.deleteSync() and file.delete() file.readAsStringSync() and file.readAsString() file.writeAsBytesSync(bytes) and file.writeAsBytes(bytes) and many, many more. What are the considerations that I should keep in mind when choosing between the sync and async options? I seem to recall seeing somewhere that the sync option is faster if you have to wait for it to finish anyway ( await file.delete() for example). But I can't

How does javascript async/await actually work?

折月煮酒 提交于 2021-02-19 04:09:49
问题 I have some code using javascript async/await: function sleep(ms) { return new Promise(resolve => setTimeout(resolve, ms)); } async function fun1() { console.log("dosomething1"); await sleep(6000); console.log("dosomething2"); return "returnfromfun1"; } console.log(fun1()); console.log("hello"); According to the official document about async/await: An async function can contain an await expression that pauses the execution of the async function and waits for the passed Promise's resolution,

How does javascript async/await actually work?

点点圈 提交于 2021-02-19 04:09:42
问题 I have some code using javascript async/await: function sleep(ms) { return new Promise(resolve => setTimeout(resolve, ms)); } async function fun1() { console.log("dosomething1"); await sleep(6000); console.log("dosomething2"); return "returnfromfun1"; } console.log(fun1()); console.log("hello"); According to the official document about async/await: An async function can contain an await expression that pauses the execution of the async function and waits for the passed Promise's resolution,