Last active
January 22, 2016 00:34
-
-
Save reactima/8d9076b70e33a47b27fc to your computer and use it in GitHub Desktop.
why i don't like yield? await a + await b to mean (await a) + (await b), but yield a + yield b means yield (a + (yield b))
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict' | |
function sum(a, b) { | |
return a+b | |
} | |
function mul(a, b) { | |
return a*b | |
} | |
async function asyncTest(a,b) { | |
//return await sum (a,b) + (await mul (a,b)) // fine, no error | |
return await sum (a,b) + await mul (a,b) // no error | |
} | |
function *yieldTest(a,b){ | |
let result | |
// return result = yield sum (a,b) + yield mul (a,b) // error | |
// return result = yield sum (a,b) + (yield mul (a,b)) // no error, but fucked-up | |
yield result = sum (a,b) | |
yield result = result + mul (a,b) | |
return result // no error | |
} | |
asyncTest(2,3).then((res) => console.log('asyncTest: '+res)) // asyncTest: 11 | |
const g = yieldTest(2,3) | |
console.log(g.next()) // { value: 5, done: false } | |
console.log(g.next()) // { value: 11, done: false } | |
console.log(g.next()) // { value: 11, done: true } | |
console.log(g.next()) // { value: undefined, done: true } | |
// * always return a generator that can be advanced (and stopped) | |
// from outside by consuming it similar to an iterator | |
// async will always return a promise that depends | |
// on other promises and whose execution is concurrent | |
// to other asynchronous operations | |
// (and might be cancelled from outside). | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment