ecmascript-2017

(ES6) class (ES2017) async / await getter

我们两清 提交于 2019-11-27 01:43:56
问题 Is it or will it be possible to have an ES6 class getter return a value from an ES2017 await / async function. class Foo { async get bar() { var result = await someAsyncOperation(); return result; } } function someAsyncOperation() { return new Promise(function(resolve) { setTimeout(function() { resolve('baz'); }, 1000); }); } var foo = new Foo(); foo.bar.should.equal('baz'); 回答1: You can do this class Foo { get bar() { return (async () => { return await someAsyncOperation(); })(); } } which

How to “await” for a callback to return?

非 Y 不嫁゛ 提交于 2019-11-26 18:47:01
When using a simple callback such as in the example below: test() { api.on( 'someEvent', function( response ) { return response; }); } How can the function be changed to use async / await? Specifically, assuming 'someEvent' is guaranteed to be called once and only once, I'd like the function test to be an async function which does not return until the callback is executed such as: async test() { return await api.on( 'someEvent' ); } async/await is not magic. An async function is a function that can unwrap Promises for you, so you'll need api.on() to return a Promise for that to work. Something

ES8 Immediately invoked async function expression

痞子三分冷 提交于 2019-11-26 16:51:10
问题 I haven't seen these constructs used much but I've found myself writing them to make use of async / await in functions that wouldn't typically return a promise, for example chan.consume(queue, (msg) => { this.pendingMsgs++; // executed immediately (async () => { await this.handleMessage(msg); this.pendingMsgs--; if (cancelled && this.pendingMsgs === 0) { await chan.close(); await this.amqpConnectionPool.release(conn); } })(); }); as opposed to chan.consume(queue, async (msg) => { // external

Async/Await inside Array#map()

孤人 提交于 2019-11-26 16:12:52
问题 I'm getting compile time error with this code: const someFunction = async (myArray) => { return myArray.map(myValue => { return { id: "my_id", myValue: await service.getByValue(myValue); } }); }; Error message is: await is a reserved word Why can't I use it like this? I also tried another way, but it gives me same error: const someFunction = async (myArray) => { return myArray.map(myValue => { const myNewValue = await service.getByValue(myValue); return { id: "my_id", myValue: myNewValue } })

How can I use async/await at the top level?

烂漫一生 提交于 2019-11-26 15:57:03
I have been going over async/await and after going over several articles, I decided to test things myself. However, I can't seem to wrap my head around why this does not work: async function main() { var value = await Promise.resolve('Hey there'); console.log('inside: ' + value); return value; } var text = main(); console.log('outside: ' + text) The console outputs the following (node v8.6.0) : > outside: [object Promise] > inside: Hey there Why does the log message inside the function execute afterwards? I thought the reason async/await was created was in order to perform synchronous

Use async await with Array.map

不问归期 提交于 2019-11-26 14:55:25
Given the following code: var arr = [1,2,3,4,5]; var results: number[] = await arr.map(async (item): Promise<number> => { await callAsynchronousOperation(item); return item + 1; }); which produces the following error: TS2322: Type 'Promise<number>[]' is not assignable to type 'number[]'. Type 'Promise<number> is not assignable to type 'number'. How can I fix it? How can I make async await and Array.map work together? Ajedi32 The problem here is that you are trying to await an array of promises rather than a promise. This doesn't do what you expect. When the object passed to await is not a

try/catch blocks with async/await

半腔热情 提交于 2019-11-26 12:55:43
I'm digging into the node 7 async/await feature and keep stumbling across code like this function getQuote() { let quote = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."; return quote; } async

How to reject in async/await syntax?

♀尐吖头ヾ 提交于 2019-11-26 10:07:14
问题 How can I reject a promise that returned by an async/await function? e.g. Originally foo(id: string): Promise<A> { return new Promise((resolve, reject) => { someAsyncPromise().then((value)=>resolve(200)).catch((err)=>reject(400)) }); } Translate into async/await async foo(id: string): Promise<A> { try{ await someAsyncPromise(); return 200; } catch(error) {//here goes if someAsyncPromise() rejected} return 400; //this will result in a resolved promise. }); } So, how could I properly reject

How can I use async/await at the top level?

烈酒焚心 提交于 2019-11-26 04:09:11
问题 I have been going over async / await and after going over several articles, I decided to test things myself. However, I can\'t seem to wrap my head around why this does not work: async function main() { var value = await Promise.resolve(\'Hey there\'); console.log(\'inside: \' + value); return value; } var text = main(); console.log(\'outside: \' + text); The console outputs the following (node v8.6.0) : > outside: [object Promise] > inside: Hey there Why does the log message inside the

Use async await with Array.map

喜欢而已 提交于 2019-11-26 04:06:11
问题 Given the following code: var arr = [1,2,3,4,5]; var results: number[] = await arr.map(async (item): Promise<number> => { await callAsynchronousOperation(item); return item + 1; }); which produces the following error: TS2322: Type \'Promise<number>[]\' is not assignable to type \'number[]\'. Type \'Promise<number> is not assignable to type \'number\'. How can I fix it? How can I make async await and Array.map work together? 回答1: The problem here is that you are trying to await an array of