async-await

Wrapping promise in async/await

ぐ巨炮叔叔 提交于 2021-02-07 03:22:02
问题 I'm struggling a bit with async/await and returning a value from a Promise. function test () { return new Promise((resolve, reject) => { resolve('Hello') }) } async function c() { await test() } As I understood things I should be able to get a value by doing: console.log(c()) But clearly I am missing a point here as this returns a promise. Shouldn't it print "hello"? On a similar note I am unclear as to whether a callback needs to be converted to a promise before wrapping it in async/await?

Wrapping promise in async/await

巧了我就是萌 提交于 2021-02-07 03:20:40
问题 I'm struggling a bit with async/await and returning a value from a Promise. function test () { return new Promise((resolve, reject) => { resolve('Hello') }) } async function c() { await test() } As I understood things I should be able to get a value by doing: console.log(c()) But clearly I am missing a point here as this returns a promise. Shouldn't it print "hello"? On a similar note I am unclear as to whether a callback needs to be converted to a promise before wrapping it in async/await?

js - How to call an async function within a Promise .then()

给你一囗甜甜゛ 提交于 2021-02-06 22:57:43
问题 First, I have to mention that I already look through many questions in stackoverflow, but many doesn't answer my question. Not to mention many doesn't even have an answer. How do I achieve the following, making sure functionB() executes after functionA() finishes? Note: I do not want to convert my async functions to new Promise(resolve=>{...}) because I'll have to convert the someServiceThatMakesHTTPCall() as well, and any other async functions within the call stack, which is a big change.

js - How to call an async function within a Promise .then()

隐身守侯 提交于 2021-02-06 22:37:27
问题 First, I have to mention that I already look through many questions in stackoverflow, but many doesn't answer my question. Not to mention many doesn't even have an answer. How do I achieve the following, making sure functionB() executes after functionA() finishes? Note: I do not want to convert my async functions to new Promise(resolve=>{...}) because I'll have to convert the someServiceThatMakesHTTPCall() as well, and any other async functions within the call stack, which is a big change.

Why doesn't an infinitely recursive async function cause stack overflow?

那年仲夏 提交于 2021-02-06 16:28:44
问题 I was thinking what happens when an async function recursively calls itself infinitely. My thought was that it will not cause stack overflow. But I can't exactly point out why this is the case. const foo = async () => { const txt = await Promise.resolve("foo"); console.log(txt); foo(); } foo(); The code above prints "foo" infinitely without overflowing the stack. My idea is that the code is conceptually similar to the following, it doesn't cause stack overflow because the recursive call to

TypeScript: cannot find name async/await

时光怂恿深爱的人放手 提交于 2021-02-06 14:53:08
问题 I'm using typescript@next and I want to compile my code to es5 , but each time I'm using async or await keywords the compiler errors with that message: Cannot find name 'await'. Heres my libs: dom , es2015 , es2016 , es2017 . Code example: let asyncFn = () => { return new Promise((resolve:Function)=>{resolve(2)}) } // should log `2` console.log(await asyncFn()) Such things are possible even with typescript@2.0.x , I've tried it, but somehow I am unable to compile my code anyway. 回答1: You need

Run “async” method on a background thread

守給你的承諾、 提交于 2021-02-06 14:28:39
问题 I'm trying to run an "async" method from an ordinary method: public string Prop { get { return _prop; } set { _prop = value; RaisePropertyChanged(); } } private async Task<string> GetSomething() { return await new Task<string>( () => { Thread.Sleep(2000); return "hello world"; }); } public void Activate() { GetSomething.ContinueWith(task => Prop = task.Result).Start(); // ^ exception here } The exception thrown is: Start may not be called on a continuation task. What does that mean, anyway?

Run “async” method on a background thread

六月ゝ 毕业季﹏ 提交于 2021-02-06 14:26:52
问题 I'm trying to run an "async" method from an ordinary method: public string Prop { get { return _prop; } set { _prop = value; RaisePropertyChanged(); } } private async Task<string> GetSomething() { return await new Task<string>( () => { Thread.Sleep(2000); return "hello world"; }); } public void Activate() { GetSomething.ContinueWith(task => Prop = task.Result).Start(); // ^ exception here } The exception thrown is: Start may not be called on a continuation task. What does that mean, anyway?

Run “async” method on a background thread

天大地大妈咪最大 提交于 2021-02-06 14:26:00
问题 I'm trying to run an "async" method from an ordinary method: public string Prop { get { return _prop; } set { _prop = value; RaisePropertyChanged(); } } private async Task<string> GetSomething() { return await new Task<string>( () => { Thread.Sleep(2000); return "hello world"; }); } public void Activate() { GetSomething.ContinueWith(task => Prop = task.Result).Start(); // ^ exception here } The exception thrown is: Start may not be called on a continuation task. What does that mean, anyway?

Ignore async without await compilation warning

爱⌒轻易说出口 提交于 2021-02-06 12:46:27
问题 I have a base controller with the following abstract method: [HttpDelete] public abstract Task<IHttpActionResult> Delete(int id); In one particular controller, I don't want to implement deletion, so the method looks like this: public override async Task<IHttpActionResult> Delete(int id) { return ResponseMessage(Request.CreateResponse(HttpStatusCode.MethodNotAllowed, new NotSupportedException())); } Although the above code compiles, I get a warning: This async method lacks 'await' operators