ecmascript-2017

How is async/await working in serial and parallel?

主宰稳场 提交于 2019-12-22 03:55:18
问题 I have two async functions. Both of them are waiting for two 3 seconds function calls. But the second one is faster than the first. I think the faster one is running in parallel and other in serial. Is my assumption correct? If yes, why is this happening as both the functions look logically same? function sleep() { return new Promise(resolve => { setTimeout(resolve, 3000); }); } async function serial() { await sleep(); await sleep(); } async function parallel() { var a = sleep(); var b =

How do i wrap a callback with async await?

两盒软妹~` 提交于 2019-12-21 04:07:12
问题 My function resolves a promise that resolves as soon as the http server starts. This is my code: function start() { return new Promise((resolve, reject) { this.server = Http.createServer(app); this.server.listen(port, () => { resolve(); }); }) } How do i convert the start function to async/await? 回答1: Include async before the function declaration and await the Promise constructor. Though note, you would essentially be adding code to the existing pattern. await converts a value to a Promise ,

async function - await not waiting for promise

流过昼夜 提交于 2019-12-18 14:08:24
问题 I'm trying to learn async-await. In this code - const myFun = () => { let state = false; setTimeout(() => {state = true}, 2000); return new Promise((resolve, reject) => { setTimeout(() => { if(state) { resolve('State is true'); } else { reject('State is false'); } }, 3000); }); } const getResult = async () => { return await myFun(); } console.log(getResult()); why am I getting output as - Promise { <pending> } Instead of some value? Shouldn't the getResult() function wait for myFun() function

async/await native implementations

人盡茶涼 提交于 2019-12-17 18:35:54
问题 This proposal suggests that async functions can use generator functions under the hood, although I cannot find a confirmation of this in ES2017 spec. Moreover, when generator prototype becomes messed up in Chrome/Node.js, async functions don't seem to be affected, this suggests that GeneratorFunction isn't used by AsyncFunction , at least directly: Object.getPrototypeOf((function * () {}).prototype).next = null; (async () => { return await Promise.resolve(1); })() .then(console.log); How

async/await always returns promise

眉间皱痕 提交于 2019-12-17 07:29:39
问题 I'm trying async/await functionality. I have such code imitating a request: const getJSON = async () => { const request = () => new Promise((resolve, reject) => ( setTimeout(() => resolve({ foo: 'bar'}), 2000) )); const json = await request(); return json; } When I use the code in this way console.log(getJSON()); // returns Promise it returns a Promise but when I call this line of code getJSON().then(json => console.log(json)); // prints { foo: 'bar' } it prints json as expected Is it

async/await always returns promise

廉价感情. 提交于 2019-12-17 07:29:00
问题 I'm trying async/await functionality. I have such code imitating a request: const getJSON = async () => { const request = () => new Promise((resolve, reject) => ( setTimeout(() => resolve({ foo: 'bar'}), 2000) )); const json = await request(); return json; } When I use the code in this way console.log(getJSON()); // returns Promise it returns a Promise but when I call this line of code getJSON().then(json => console.log(json)); // prints { foo: 'bar' } it prints json as expected Is it

How to use ES8 async/await with streams?

╄→尐↘猪︶ㄣ 提交于 2019-12-17 07:14:26
问题 In https://stackoverflow.com/a/18658613/779159 is an example of how to calculate the md5 of a file using the built-in crypto library and streams. var fs = require('fs'); var crypto = require('crypto'); // the file you want to get the hash var fd = fs.createReadStream('/some/file/name.txt'); var hash = crypto.createHash('sha1'); hash.setEncoding('hex'); fd.on('end', function() { hash.end(); console.log(hash.read()); // the desired sha1sum }); // read all file and pipe it (write it) to the hash

Is there a way to wrap an await/async try/catch block to every function?

两盒软妹~` 提交于 2019-12-17 06:15:00
问题 So i'm using express.js and looking into using async/await with node 7. Is there a way that I can still catch errors but get rid of the try/catch block? Perhaps a function wrapper? I'm not sure how this would actually execute the function's code and also call next(err) . exports.index = async function(req, res, next) { try { let user = await User.findOne().exec(); res.status(200).json(user); } catch(err) { next(err); } } Something like this...? function example() { // Implements try/catch

How to “await” for a callback to return?

放肆的年华 提交于 2019-12-17 03:34:30
问题 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' ); } 回答1: async/await is not magic. An async function is a

async/await function does not wait for setTimeout to finish

梦想与她 提交于 2019-12-13 19:55:56
问题 I'm using await within an async function execute functions in a particular order, if you see here - I wanted startAnim to wait until hideMoveUI had finished executing to execute itself. Though my console log returns: startAnim hideMoveUI My code: async function printAll() { await hideMoveUI(); await startAnim(); } printAll(); hideMoveUI = () => { setTimeout(() => { console.log('hideMoveUI'); }, 3000); } startAnim =() => { setTimeout(() => { console.log('startAnim'); }, 500); } Is setTimeout